Android: How to avoid errors on unlockCanvasAndPost method?

妖精的绣舞 提交于 2019-12-24 20:13:47

问题


Could anyone, please, help me to explain the following.

How properly call the holder.unlockCanvasAndPost(canvas) method to avoid the errors like "Surface:unlockCanvasAndPost failed, no locked buffer"?

I mean, is there any way to check that canvas has been locked before?

Currently, I have:

try{
_surfaceHolder.unlockCanvasAndPost(canvasToUnlock);
}
catch(Throwable t){ }

But I guess this is not proper.

Thanks in advance.

Answer on @njzk2 post:

Yes. But in my case I faced with necessity to say to the handler, that I use for unlocking the canvas from different threads, when it should be locked and unlocked. I mean:

    class MyObject extends Object{
        private Canvas canvas = null;
        private SurfaceHolder holder = null;

        MyObject(SurfaceHolder sh, Canvas cv){
            canvas = cv;
            holder= sh;
        }

        public SurfaceHolder getHolder() {return holder;}
        public Canvas getCanvas() {return canvas;}
    };

    private final Handler mHandler = new Handler(){

        public void handleMessage(android.os.Message msg){
            MyObject myObject= (MyObject)msg.obj;

            switch(msg.what){
                case 1:
                    Canvas c = myObject.getCanvas();
                    c = myObject.getHolder().lockCanvas();
                    break;
                case 2:
                    myObject.getHolder().unlockCanvasAndPost(myObject.getCanvas());
                    break;
            }
        };
    };

For example, when the fade-in/out effects are produced with current and next images, I can realize double tap from the main thread to change image immediately, that sometimes being imposed to stop the cycle before canvas will be unlocked.

So that, I need to prevent an unexpected locking by knowing, was it unlocked before, and conversely.

Again, thanks in advance for any and all replies.

来源:https://stackoverflow.com/questions/20132262/android-how-to-avoid-errors-on-unlockcanvasandpost-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!