1

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?

Daniel Gray
  • 1,697
  • 1
  • 21
  • 41
  • Don't create Bitmap and Canvas objects again and again if you are reusing. If blackRect is used for screen clearing then why is it not starting from 0,0? – resw67 Apr 25 '17 at 22:47
  • I use `blackRect` for coloring the part that is not colored (`widthToPaint` is the dividing X coordinate where the colored part ends and the black part starts). – Daniel Gray Apr 25 '17 at 23:21
  • 1
    That is why i asked that. Paint everyting to black first then paint some part of it to any color you want. – resw67 Apr 25 '17 at 23:48
  • Tried that and it still looks the same :( I guess I am missing something else... – Daniel Gray Apr 26 '17 at 20:40

0 Answers0