Android游戏开发之SurfaceView动画
2010-09-10 01:09:00 来源:WEB开发网start()启动循环,并通过适当的方式结束循环进而结束整个线程。还要注意一点,所有对
Surface 的操作都必须要保证同步,因此我们会使用Synchronized 关键字,同步SurfaceHolder。
增加了GameThread 后的代码如下:
public class GameView extends SurfaceView implements Callback {
public static final String tag = "GameView";
//声明GameThread类实例
GameThread gameThread;
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
//获取SurfaceHolder
SurfaceHolder surfaceHolder = getHolder();
//添加回调对象
surfaceHolder.addCallback(this);
//创建GameThread类实例
gameThread = new GameThread(surfaceHolder);
}
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
Log.v(tag, "surfaceChanged");
}
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.v(tag, "surfaceCreated");
//启动gameThread
gameThread.start();
}
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.v(tag, "surfaceDestroyed");
//通过结束run()函数的方法结束gameThread,详见GameThread类的定义
gameThread.run = false;
}
/**
* GameThread的定义
* @author xingye
*
*/
class GameThread extends Thread {
SurfaceHolder surfaceHolder;
//run()函数中控制循环的参数。
boolean run = true;
public GameThread(SurfaceHolder surfaceHolder) {
this.surfaceHolder = surfaceHolder;
}
@Override
public void run() {
// TODO Auto-generated method stub
更多精彩
赞助商链接