Friday, October 2, 2009

Rotating a bitmap in Android





Here, we have an image of globe and I am going to rotate it.

In Android, we can rotate a bitmap in canvas. Its simple, but we have to know the save() and restore() methods in Canvas class.

We can load a bitmap using


Bitmap globe = BitmapFactory.decodeResource(context.getResources(), R.drawable.globe);


in onDraw() method,

we can rotate the bitmap using

canvas.roatate() method. But here the problem is, the whole canvas will rotate. But by using, save() and restore() we can solve this problem.

Before rotating the, just call


canvas.save() then, call
cavas.rotate(degree)
degree+=5;

then, draw the image. After drawing the image, just call canvas.restore().

This will solve the problem of rotating the whole canvas.

To show, canvas is restored, I am drawing a text after drawing bitmap in canvas by,
canvas.drawText("Here our globe is rotating", 50, 400, textColor);
In the end of onDraw, just call invalidate();

Now, our globe will rotate.





and the text under the globe is static.

This will do.

If you have any queries, about my post, feel free to contact @ nithin.warier@gmail.com

Happy to help You....

No comments:

Post a Comment

Androidians