问题
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