Android中的2D游戏开发

开发思路:

第一个Activity中,设置游戏全屏,设置初始化ContentView,

在CcontentView中,继承SurfaceView,实现Callback和Runnable接口

重写方法,在surfaceCreated方法中启动线程,在线程中给画布画画。

第一个类:SurfaceViewTestActivity

package cn.edu.surfacetest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class SurfaceViewTestActivity extends Activity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); //drop the application title
        //hide the statubar(battery and flag)
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new MySurfaceView(this));
    }
}
 

第二个类MySurfaceView:

package cn.edu.surfacetest;


import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MySurfaceView extends SurfaceView implements Callback,Runnable{
	private Activity father;
	private SurfaceHolder sfh;
	private Canvas canvas;
	public MySurfaceView(Context context) {
		super(context); 
		// TODO Auto-generated constructor stub
		sfh=this.getHolder();
		sfh.addCallback(this);
		father=(Activity)context;	
	
	}	


	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}


	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		new Thread(this).start();
	}

	
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	public void myDraw(){
		canvas=sfh.lockCanvas();
		Paint paint=new Paint();
		paint.setColor(Color.RED);
		Resources resource=this.getResources();
		Bitmap bitmap=BitmapFactory.decodeResource(resource, R.drawable.icon);
		Matrix matrix=new Matrix();
		matrix.setScale(1,1);
		canvas.drawBitmap(bitmap, matrix, paint);
		canvas.drawRect(10, 20, 30,40, paint);
		canvas.drawText("Hello", 20, 20, paint);
		sfh.unlockCanvasAndPost(canvas);
	}

	
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		father.setContentView(new MySurface2((Context)father));
		return super.onTouchEvent(event);
	}

	
	public void run() {
		// TODO Auto-generated method stub
		myDraw();
		System.out.println("begin");
	}
	
}

第三个类MySurface2:

package cn.edu.surfacetest;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MySurface2 extends SurfaceView implements Callback,Runnable{
	private SurfaceHolder sfh;
	private Canvas canvas;
	public MySurface2(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		sfh=this.getHolder();
		sfh.addCallback(this);
	}
	
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		new Thread(this).start();
	}

	
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}
	public void myDraw(){
		canvas=sfh.lockCanvas();
		Paint paint=new Paint();
		paint.setColor(Color.GREEN);
		Resources resource=this.getResources();
		Bitmap bitmap=BitmapFactory.decodeResource(resource, R.drawable.icon);
		Matrix matrix=new Matrix();
		matrix.setScale(1,1);
		canvas.drawBitmap(bitmap, matrix, paint);
		canvas.drawRect(10, 20, 30,40, paint);
		canvas.drawText("Hello", 20, 20, paint);
		sfh.unlockCanvasAndPost(canvas);
	}

	
	public void run() {
		// TODO Auto-generated method stub
		myDraw();
		System.out.println("begin");
	}
	
}
 

相关推荐