0

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); } 
1

1 Answer 1

1

Well i can't figure out functionality of your code as its a snippet from a section, But what I can figures out is the reason. quick check

  1. If you trying to draw bitmaps from a view, make sure drawing cache is enabled/
  2. canvas.drawBitmap , this is the API for drawing a Bitmap over a canvas and not setBitmap

you can use save and restore method of canvas accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

I found this post and it solve my problem and start drawing groups.google.com/forum/#!topic/android-beginners/6pO8SJN3CTY I will update the post with the working code, and Thanks for your reply

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.