Kalina
Kalina

Reputation: 5594

Am I not doing correctly or understanding double buffering on Android?

I have a function

@Override
public void run() {
    while(running && (!eof)){
        if(surfaceHolder.getSurface().isValid()){
            Canvas canvas = surfaceHolder.lockCanvas();
            paint(canvas);
            surfaceHolder.unlockCanvasAndPost(canvas);  
        }
    }
    thread = null;
}

where paint(canvas) calls a bunch of other functions that draw a graph and text, for example

canvas.drawText("Time="+myRecord.getMyTime(), 100, 100, paint);

The problem I'm having is that the graph and the text, both of which should be constantly changing, don't get erased but instead keep drawing over themselves. Shouldn't my entire canvas get redrawn every time because that's how double buffering works with the lock() and unlock()? Am I not understanding this correctly? How am I supposed to do this?

Upvotes: 2

Views: 399

Answers (1)

mibollma
mibollma

Reputation: 15118

You need to clear the Canvas yourself after lockCanvas() using Canvas.drawColor().

This might be relevant too:

The content of the Surface is never preserved between unlockCanvas() and lockCanvas(), for this reason, every pixel within the Surface area must be written. The only exception to this rule is when a dirty rectangle is specified, in which case, non-dirty pixels will be preserved.

Source

Upvotes: 2

Related Questions