买了本android应用开发揭秘关于游戏开发那章的问题
在surfaceView类开发框架中
package y.b;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;/** * @author yubin * @version 2012-9-11 上午10:08:22 **/public class Gamesurfaceview extends SurfaceView implements Callback, Runnable { // 控制循环 boolean mbLoop = false; SurfaceHolder mSurfaceHolder = null; int miCount = 0; int x = 50, y = 50; public Gamesurfaceview(Context context) { super(context); // 得到SurfaceHolder实例 mSurfaceHolder = this.getHolder(); // 添加回调函数 创建修改销毁方法 mSurfaceHolder.addCallback(this); // 只有当该View获得焦点时才会调用onKeyDown方法 this.setFocusable(true); mbLoop = true; } @Override public void run() { while(mbLoop) { try { Thread.sleep(200); } catch(Exception e) { } synchronized(mSurfaceHolder) { Draw(); } } } private void Draw() { // 锁定画布,得到Canvas Canvas canvas = mSurfaceHolder.lockCanvas(); if(miCount < 100) { miCount++; } else { miCount = 0; } // 设置画笔 Paint mpaint = new Paint(); // 设置无锯齿 mpaint.setAntiAlias(true); mpaint.setColor(Color.BLACK); // 清屏 canvas.drawRect(0, 0, 320, 480, mpaint); switch(miCount % 4) { case 0: mpaint.setColor(Color.BLUE); break; case 1: mpaint.setColor(Color.GREEN); break; case 2: mpaint.setColor(Color.RED); break; case 3: mpaint.setColor(Color.YELLOW); break; default: mpaint.setColor(Color.WHITE); break; } // 画实心矩形 canvas.drawCircle((320 - 25) / 2, y, 50, mpaint); // 解锁画布绘制 mSurfaceHolder.unlockCanvasAndPost(canvas); } // surface创建时激发 @Override public void surfaceCreated(SurfaceHolder holder) { // 开启绘图线程 new Thread(this).start(); } // surface改变时激发 @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } // surface销毁时激发 @Override public void surfaceDestroyed(SurfaceHolder holder) { // 停止循环 mbLoop = false; }}