当前位置:操作系统 > 安卓/Android >>

【Android游戏开发十九】(必看篇)Su易做图ceView运行机制详解

 

 

                                                    切入后台操作比如点击HOME按键,点击返回按键...

 

   那么重新进入程序报异常主要Su易做图ceiew 有两点会报异常:

 

第一:提交画布异常!如下图(模拟器错误提示,以及Logcat Detail)

 


 

\\

 

 

解决代码:

 

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

public void draw() { 

        try { 

            canvas = sfh.lockCanvas(); 

            if (canvas != null) { 

                canvas.drawColor(Color.WHITE); 

                canvas.drawBitmap(bmp, bmp_x, bmp_y, paint); 

            } 

        } catch (Exception e) { 

            Log.v("Himi", "draw is Error!"); 

        } finally {//备注1 

            if (canvas != null)//备注2 

                sfh.unlockCanvasAndPost(canvas); 

        } 

    } 

 

先看备注1这里,之前的文章中我给大家解释过为什么要把sfh.unlockCanvasAndPost(canvas); 写在finally中,主要是为了保证能正常的提交画

布.今天主要说说备注2,这里一定要判定下canvas是否为空,因为当程序切入后台的时候,canvas是获取不到的!那么canvas一旦为空,提交画

布这里就会出现参数异常的错误!

 

 

下面来说另外一种情况:线程启动异常!如下图(模拟器错误提示,以及Logcat Detail)

 

\

\

 

 

这种异常只是在当你程序运行期间点击Home按钮后再次进入程序的时候报的异常,异常说咱们的线程已经启动!为什么返回按钮就没事?

 

OK,下面我们就要来先详细讲解一下Android中Back和Home按键的机制!然后分析问题,并且解决问题!

 

先看下面MySu易做图ceViewAnimation.java的类中的代码:

 

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

public class MySu易做图ceViewAnimation extends Su易做图ceView implements Callback, Runnable { 

    private Thread th; 

    private Su易做图ceHolder sfh; 

    private Canvas canvas; 

    private Paint paint; 

    private Bitmap bmp; 

    private int bmp_x, bmp_y; 

    public MySu易做图ceViewAnimation(Context context) { 

        super(context); 

        this.setKeepScreenOn(true); 

        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.himi_dream); 

        sfh = this.getHolder(); 

        sfh.addCallback

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,