# How to rotate a moving canvas in Android

I am developing an Android game. In this game some trains are moving on tracks. I am able to move trains horizontally and vertically using two train images for horizontal train and vertical train separately.

Now I want to move a train that is coming on horizontal track to vertical, I want to visualize it as it is now taking turn and moving on vertical track.

How can I do this, do I need to use so many images for each turning pixel, or is there any other way to turn the train?

You can rotate the whole Canvas Object. Just look at the Canvas documentation in Android SDK Documentation Canvas.

You might want to to something like:

canvas.save();
canvas.rotate(90);
canvas.drawBitmap(...);
canvas.restore();


First, you save the current Canvas (or Matrix), then you rotate the whole thing, draw the image, and then you restore the Canvas orientation. Your drawing operations will be saved. One more thing: There is a method

canvas.rotate(degree, px, py)


this will rotate the canvas around the point (px,py). Could be better suited for your needs.