Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i use canvas.save and canvas.restore? [duplicate]

Possible Duplicate:
Can i save lots of bitmaps to one bitmap? (2d)

I wonder how canvas.save and canvas.restore really works.

how i want it to work, and how i use it (but doesnt work).

  1. lock the canvas
  2. do some drawing with out unlockandpost
  3. canvas.save() (store the int)
  4. Do some more drawings
  5. Post the canvas
  6. Restore the canvas from step 3
  7. Do some more drawings, repeat from 6 (loop)

What i really need is to save my canvas at a certain stage ( the background), and then draw objects above it, without having to draw the background everytime i want to update my canvas.

like image 348
Marcus Brunsten Avatar asked Nov 01 '25 09:11

Marcus Brunsten


1 Answers

Canvas.save() & restore() don't act on the bitmap attached to the canvas... they exist to control aspects of the canvas drawing environment, specifically the current clipping area and the matrix.

You'd use save() and restore if you wanted to, say, draw a rotated sprite. To do that, you'd first save() the current canvas state, then you'd translate() so that the origin - pixel address (0,0) - is where you want the sprite to go, then you'd rotate(), and then you can drawBitmap(). Finally you can restore() the drawing environment back to normal.

So you're basically doomed to draw the background every time. If this is a complex operation, store it in an offscreen bitmap. So long as the background can be drawn in a single operation (drawBitmap, say) performance shouldn't suffer too much.

like image 146
Reuben Scratton Avatar answered Nov 03 '25 23:11

Reuben Scratton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!