1

I would like to implement a custom View with smooth scrolling feature. I have read Android View.onDraw() always has a clean Canvas. By building an off-screen bitmap, it even take some more time to draw the off-screen bitmap. Therefore, I have think of using the view drawing cache.

@Override
public void onDraw(Canvas canvas){
    long start = System.nanoTime(); 
    if(redrawAll){
        drawFullScreen(canvas);
        handler.post(new Runnable(){
            @Override
            public void run() {
                cachedBitmap = SampleView4.this.getDrawingCache();
            }                   
        });
    }
    else{
        if(cachedBitmap != null && !cachedBitmap.isRecycled()){
            canvas.drawBitmap(cachedBitmap ,scrollByX, scrollByY, p);
            drawPartOfScreen(canvas, scrollByX, scrollByY);
        }
        handler.post(new Runnable(){
            @Override
            public void run() {
                cachedBitmap = SampleView4.this.getDrawingCache();
            }                   
        });
    }
    Log.d(TAG, "drawTiles take: "+(System.nanoTime() - start) + " ns");     

}

It probably work but have strange behaviour. Sometime, the cached Bitmap do no change after onDraw() and thus look very weird.

And, the main problem is the performance do not have not much different with just call drawFullScreen(canvas); each time the view scrolls.

Am I on the right track? I have develop on Android 4 so I turn on the hardware acceleration. But the docs say the view has cached in hardware-texture. Do I really need drawing cache in hardware acceleration environment?

Community
  • 1
  • 1
Yeung
  • 2,202
  • 2
  • 27
  • 50

1 Answers1

0

I think you can't rely on drawing cache when hardware acceleration is ON. According the doc about View.setDrawingCacheEnabled() :

"Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers."

ggurov
  • 1,496
  • 2
  • 15
  • 21