I am writing a small animation where an area should start on the left hand side of the screen, and widen to cover the entire screen, and bounce back. I want the speed to vary, but I am finding that when the speed is high, there are many "ghost" images appearing.
I tried with a basic View
at first, and then changed to a SurfaceView
. It was still ghosty. I looked into drawing on a separate bitmap (buffer) as presented in this question.
My current version of the drawing code looks like this:
Bitmap tempCanvasBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas screenCanvas = currentCanvasHolder.lockCanvas();
Canvas buffCanvas = new Canvas();
buffCanvas.setBitmap( tempCanvasBitmap );
blackRect.set( widthToPaint + 1, 0, viewWidth, viewHeight );
colorRect.set( 0, 0, widthToPaint, viewHeight );
buffCanvas.drawRect( blackRect, blackPaint );
buffCanvas.drawRect( colorRect, colorPaints[ currentColor ] );
screenCanvas.drawBitmap( tempCanvasBitmap, 0, 0, new Paint());
currentCanvasHolder.unlockCanvasAndPost( screenCanvas );
But it is still being ghosty! To aid in explaining, I have attached a couple videos of the animation in its current state:
In trying to understand the problem more, I created a new project with a line which bounces back and forth. As recommended by the Android documentation regarding buffering, I am clearing the entire canvas each time. However, I still am seeing the line "repeated" several times throughout the animation (after it stops it's only appearing by itself, which is what I expect).
The project can be found at https://github.com/thedanielgray/android-flickering-demo . Does Android combine all the previous buffers somehow while rendering the animation? I tried to draw several lines without clearing the canvas and it seemed that there were more than 2 canvases/buffered that were being exchange at the different steps of the animation. What options to I have to control this buffering behavior?