Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw on Bitmap in android?

I'm trying to figure out how to draw on a bitmap in android, and keep a copy of these changed bitmaps for an undo function.

Bitmap b = ...
Paint p = new Paint();
canvas.drawBitmap(b, new Matrix(), null);
canvas.drawCircle(0,0,20,20);
//does Bitmap b have the circle drawn on it next time?

Or how do I get the bitmap after its been drawn on with the canvas(I want to preserve a stack of bitmaps with the changes applied by canvas drawing)? Maybe I'm going about this entirely wrong.

like image 822
Scott K Avatar asked Nov 11 '10 23:11

Scott K


1 Answers

Use new Canvas(Bitmap bitmap) to provide a Canvas with a Bitmap which will contain the result of your drawing operations.

The original Bitmap that you draw on your Canvas with drawBitmap will never be modified.

After each operation done by the user you might:

  • keep in memory a list of the operations done
  • save intermediate results to external storage with Bitmap.compress

Another approach could be to use a LayerDrawable to stack successive drawing operations on top of each other. You can imagine allowing the user to disable each individual operation done.

like image 113
Kevin Gaudin Avatar answered Oct 08 '22 01:10

Kevin Gaudin