What does canvas.translate do?
Even when I first answered this question a few years ago, I didn't really understand how Canvas
transforms (like translate
, rotate
, etc.) worked. I used to think that translate
moved the thing that you were drawing. Actually, translate
moves the entire coordinate system. This has the desired effect of also moving the thing you are drawing.
On your screen it looks like you are moving the drawing:
What is actually happening is you are moving the coordinate system to a new place on the canvas:
I draw the tree first at (0,0)
. Then I translate the origin of the coordinate system to some other spot on the canvas. Then I draw the tree again at (0,0)
. This way my drawing code doesn't have to change anything at all. Only the coordinate system changes.
Normally (0,0)
is at the top left of your view. Doing Canvas.translate
moves it to some other part of your view.
Saving and Restoring the Coordinate System
You can do save()
and restore()
to get back to your original coordinate system.
// draw the tree the first time
canvas.drawBitmap(tree, 0, 0, mPaint);
// draw the tree the second time
canvas.save();
canvas.translate(dx, dy); // dx = change in x, dy = change in y
canvas.drawBitmap(tree, 0, 0, mPaint); // draw still thinks it is at (0,0)
canvas.restore(); // undo the translate
When you restore
, the drawing is already on the canvas. Restoring doesn't change that. It just moves the coordinate system back to wherever it was when you saved it.
Why Translate
Note that you could achieve the same affect by changing the x,y coordinates of the draw method:
// draw the tree the first time
canvas.drawBitmap(tree, x, y, mPaint);
// update the x,y coordinates
x += dx;
y += dy;
// draw the tree the second time
canvas.drawBitmap(tree, x, y, mPaint);
This might be more intuitive coming from a math background. However, when you are translating, rotating, and scaling your image, it is often much easy to keep your drawing logic the same and just transform the canvas. Recalculating x
and y
for every draw could be very expensive.
Imagine it's a Print Head.
The easiest way to explain this is to imagine it is the print head of an inkjet or 2D printer.
translate
basically "moves" the print head along the X and Y axes the number of pixels that you tell it to.
The resulting position becomes the new "origin" (0,0).
Now that we understand that, let's make a simple face by doing the following:
Starting Origin:
x
The x
will (roughly) represent where the origin (or print head) is.
Draw a rectangle for the left eye:
canvas.drawRect(10, 10, 10, 10, paint);
x__
|__|
Note: The "origin" has not changed, it is still at the top left of this rectangle.
Move "origin" right by 20 points:
canvas.translate(20, 0)
__ x
|__|
Draw the right eye using the exact same rectangle command:
canvas.drawRect(10, 10, 10, 10, paint);
__ x__
|__| |__|
Move "origin" back to the original X position and move it down on the Y axis:
canvas.translate(-20, 20) // Positive numbers for the second param is "down" on the y-axis.
__ __
|__| |__|
x
And draw a mouth to finish it off:
canvas.drawLine( 0, 0, 30, 0, paint );
__ __
|__| |__|
x___________
Now just move the "origin" down 20 points to showcase our masterpiece:
canvas.translate(0, 20)
__ __
|__| |__|
___________
x
Not perfectly to scale as there's only so much you can do with a monospace font. :)