Android SurfaceView/Canvas flickering after trying to clear it

Posted by Mark D on Stack Overflow See other posts from Stack Overflow or by Mark D
Published on 2011-07-27T15:48:16Z Indexed on 2012/11/11 11:02 UTC
Read the original article Hit count: 127

Filed under:
|
|
|

So I am trying to clear the Canvas using canvas.drawColor(Color.BLACK) but if I just call this once, the display flickers and displays the old drawing which should have been covered up by the drawColor.

Here is the important bits of my code -

    public void update() {


    //This method is called by a Thread                


    Canvas canvas = holder.lockCanvas(null);
    if (canvas != null) {
        onDraw(canvas);
    }
    holder.unlockCanvasAndPost(canvas);
}

@Override
protected void onDraw(Canvas canvas) {

    if (toClear) {
        canvas.drawColor(Color.BLACK);

            //if this is not set to change back to false, it does not flicker
        toClear = false;
    }

    //Draw some objects that are moving around
}
public void clearScreen() {


    //This method is called when the user pressed a button

    toClear = true;
}

After Googling around a litte, I heard about double buffering but came to the understanding that lockCanvas() and unlockCanvasAndPost() should handle this for me. What is going wrong here?

© Stack Overflow or respective owner

Related posts about java

Related posts about android