新建Android项目 添加MainActivity类 // file:MainActivity.java package app.mr; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new GameView(this)); } 添加GameView类 // file: GameView.java package app.mr; import java.util.Random; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class GameView extends SurfaceView implements SurfaceHolder.Callback{ SurfaceHolder holder; public GameView(Context context) { super(context); holder = getHolder(); holder.addCallback(this); setFocusable(true); //设置焦点 } //绘制线程 class MyThread implements Runnable { public void run() { Canvas canvas = null; Paint paint = new Paint(); paint.setAntiAlias(true); long lastTime = System.nanoTime(); while (true) { try { // 计算帧率 //long time = System.nanoTime(); //float fps = 1000000000.0f/(time-lastTime); //lastTime = time; canvas = holder.lockCanvas(null); canvas.drawARGB(255, 0, 0, 0); //paint.setColor(Color.BLUE); //paint.setTextSize(24); //canvas.drawText("FPS:"+(int)fps, 30, 40, paint); } catch(Exception e) { } finally { if (canvas!=null) { holder.unlockCanvasAndPost(canvas); } } } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub new Thread(new MyThread()).start(); //Thread t = new Thread(new MyThread()); //t.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }