Save canvas then restore, why is that?

Solution 1:

I understand this question is a bit dated, but for anyone still looking for an answer I can ELI5;

Imagine the canvas is a piece of paper, and you're tasked with drawing a picture of a robot right side up, at the bottom, and another robot upside down, slightly moved to the right, and about 40% smaller at the top. Something like this; Android canvas example

How would you start? What's easier to do first?

You would probably draw the bigger robot at the bottom first since it's right-side up and it's a lot easier to draw in the direction that feels more natural. So you've got the first one done, now how do you approach the second upside down robot?

  • You could attempt to draw it as is, but that would be a bit difficult since you're upside down.

or

  • You could rotate your paper 180°, move your starting point a bit, and start drawing at a smaller scale, and after you're all done you'd just rotate the paper back.

This is what canvas.save() and canvas.restore() do, they allow you to modify your canvas in any way that makes it easier for you to draw what you need. You don't need to use these methods, but they sure do simplify a lot of the process. The above would look something like

drawRobot()
canvas.save()
canvas.rotate(180)
canvas.translate(100, 0)
canvas.scale(40,40)
drawRobot()
canvas.restore()

If we look at the restore() documentation it says

is used to remove all modifications to the matrix/clip state since the last save call

and to see what those modifications are we take a look at save() it says

translate, scale, rotate, skew, concat or clipRect, clipPath

Well look at that, we did in fact use translate rotate and scale but we also did call drawRobot() so wouldn't calling restore erase our drawing? No, because it doesn't affect the drawing, only the modifications. So when we call restore it will return our canvas to the state that it was in before we started the second drawing.

Solution 2:

What's the point of undoing what we just did!

You're not, though. If you're just going off the words, it does sound like that's what might happen, but it actually isn't.

Think of it like this:

You have a series of really complex translations and rotations you want to apply in the same onDraw(Canvas) call. Now, since every translation/rotation you apply to the Canvas happens in order, you would have to undo your last adjustments to the Canvas, or somehow calculate your new adjustments based off the previous one before drawing whatever it is you want to draw. That would get very messy, very quickly.

Using canvas.save() and canvas.restore() is a ridiculously easy way to simplify that process.

By doing adjustments that apply to the Canvas within a save/restore block, you're effectively isolating said adjustments so that whatever you want to draw next won't be affected by what you're drawing now.

Now, a little better explanation of the names:

canvas.save() is saying that I want to save the state of the current Canvas's adjustments so that I can go back to it later.

canvas.restore() is saying that I want to revert my Canvas's adjustments back to the last time I called cavas.save()

The beauty of this is in its simplicity. If you already drew whatever it is you wanted to draw during the save/restore block and you no longer need those adjustment's for your next drawing, using this let's you throw away those unnecessary adjustments and return to the state you want to start your next drawing from.

Hopefully that helps explain it!

Solution 3:

Working with the canvas involves all manner of translate,scale,rotate,skew procedures on the canvas. The save() method preserves a state before any of the aforementioned augmentation in place, restore() rewinds to a state in time where no augmentation is injected. In other words, you can save a pre state before any transformation of the canvas, do your rotations and whatever else you want to during the process, but when your finished rewind to the state before any augmentation.