I'm trying to use onDraw method and canvas to draw some items in a bitmap and cache it to draw it again and don't call onDraw again
This is part of my code :
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mCacheDrawing == null) { mCacheDrawing = Bitmap.createBitmap(mScrollWidth, mScrollHeight, Config.RGB_565); canvas.setBitmap(mCacheDrawing); for (int i = 0; i < mIcons.size(); i++) { prepareItem(canvas, paint, mIcons.get(i)); } canvas.save(); } else { canvas.setBitmap(mCacheDrawing); } } The code is not working and show me an empty screen, can any one help me please?
EDIT : I have found the following post and it help me to solve the problem >> https://groups.google.com/forum/#!topic/android-beginners/6pO8SJN3CTY
and my working code now is following :
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mCacheDrawing == null) { mCacheDrawing = Bitmap.createBitmap(mScrollWidth, mScrollHeight, Config.RGB_565); mCanvas = new Canvas(mCacheDrawing); for (int i = 0; i < mIcons.size(); i++) { prepareItem(mCanvas, paint, mIcons.get(i)); } } canvas.drawBitmap(mCacheDrawing,new Rect (0,0, mScrollWidth, mScrollHeight), new Rect (0,0, mScrollWidth, mScrollHeight), paint); }