Scott K
Scott K

Reputation: 141

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.

Upvotes: 14

Views: 53869

Answers (2)

gingo
gingo

Reputation: 3169

You can see complete guide how to draw text here:

https://www.skoumal.net/en/android-how-draw-text-bitmap/

Long story short:

Copy your bitmap to make it mutable and create Canvas based on it.

Upvotes: 0

Kevin Gaudin
Kevin Gaudin

Reputation: 9945

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.

Upvotes: 22

Related Questions