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"); } }
相关推荐
nickey 2012-05-26
琴弦第七 2012-07-11
jackeysion 2016-03-21
peixiaopao 2015-01-26
lerayZhang 2014-02-15
wocao 2013-02-17
梦想是会飞的猪 2012-09-21
xuguolibeyondboy 2012-09-14
sunstarchen 2012-08-07
做好我自己 2012-07-11
lxrlxr 2012-07-11
fzfreeworld 2012-05-21
yxx0 2012-04-26
scott0 2012-04-24
PrisonJoker 2012-03-27
ohdajing 2012-02-15