引用:http://www.linuxidc.com/Linux/2011-11/48318.htm
// 设置Paint为无锯齿
32 mPaint.setAntiAlias(true);
全文
症状:游戏过程中,按下Home键返回手机主菜单,再点击游戏图标试图返回游戏的时候屏幕黑的一片!
以前一直没在意只有结束线程在运行游戏,今天觉得改仔细琢磨一下这个问题了!
首先第一件事:打印Logo看看按下Home键后会调用哪些方法,结果如下:
游戏过程中按下Home后:
11-29 20:42:07.090: I/System.out(18835): Activity onPause...
11-29 20:42:14.190: I/System.out(18835): Activity onStop...
11-29 20:42:14.260: I/System.out(18835): SurfaceView surfaceDestroyed...
可见SurfaceView 在返回手机主菜单的时候被销毁了,而我的SurfaceView 主线程是在构造方法里创建的
那么我们返回游戏的时候会调哪些方法呢?接着看Log:
11-29 20:48:06.940: I/System.out(18835): Activity onRestart...
11-29 20:48:06.950: I/System.out(18835): Activity onResume...
11-29 20:48:07.230: I/System.out(18835): SurfaceView surfaceCreated...
11-29 20:48:07.240: I/System.out(18835): SurfaceView surfaceChanged...
surfaceDestroyed 的时候线程已经退出了运行,这时再返回到游戏刷屏的线程就没了,任何绘制方法都没调用,所以你看到的只有黑漆漆的屏幕啦!
明白了运行原理我们就知道改怎么做啦!
首先创建线程放在 surfaceCreated ,并启动线程,
当 surfaceDestroyed 调用的时候,线程已经无效了,我们把线程对象设为null释放他,
然后不管怎么返回线程都会继续运行你的游戏也就不会中断了,来段简单的代码更具表达力度!
- package com.game.view;
-
- import Android.content.Context;
- import Android.graphics.Canvas;
- import Android.graphics.Paint;
- import Android.graphics.Paint.Style;
- import Android.graphics.Rect;
- import Android.view.SurfaceHolder;
- import Android.view.SurfaceHolder.Callback;
- import Android.view.SurfaceView;
-
- public class TestView extends SurfaceView implements Callback, Runnable{
- public static final int GAME_HEART = 1000/30;
-
- public static int screenW, screenH;
-
- private Thread thread;
- private SurfaceHolder holder;
- private Paint paint;
-
- public TestView(Context context) {
- super(context);
-
- holder = getHolder();
- holder.addCallback(this);
-
- paint = new Paint(Paint.ANTI_ALIAS_FLAG);
- paint.setStyle(Style.FILL);
- paint.setTextSize(16);
- }
-
-
-
-
- private void update() {
-
- }
-
-
-
-
- private Rect rect = new Rect();
- private void draw(){
- Canvas canvas = holder.lockCanvas();
-
- String text = "天使之翼的示例Demo";
-
- paint.getTextBounds(text, 0, text.length(), rect);
-
- paint.setColor(0xfff000f0);
- canvas.drawText(text,
- (screenW - rect.width())/2,
- screenH/2 + rect.height()/2,
- paint);
-
- holder.unlockCanvasAndPost(canvas);
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
-
- screenW = getWidth();
- screenH = getHeight();
-
- thread = new Thread(this);
- isRun = true;
- thread.start();
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
-
-
- screenW = getWidth();
- screenH = getHeight();
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
-
- isRun = false;
- thread = null;
- }
-
- private boolean isRun;
- private int useTime;
- @Override
- public void run() {
-
- long start, end;
-
- while(isRun){
- start = System.currentTimeMillis();
- update();
- draw();
- end = System.currentTimeMillis();
- useTime = (int) (end - start);
-
- if(useTime < GAME_HEART){
- try {
- Thread.sleep(GAME_HEART - useTime);
- } catch (InterruptedException e) {
-
- e.printStackTrace();
- }
- }
- }
- }
- }