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?