views:

355

answers:

2

Hi All,

I need a small help on rotating one image around its center of axis among multiple images which are drawn to canvas in android.

I am loading images to canvas like below.

canvas.drawBitmap(mMachineBackground, 0, 0, null);
canvas.drawBitmap(mMachineRotator, 0, 0, null);

I want to rotate only the second bitmap around its center of axis instead of rotating the entire canvas(which includes first bitmap also).

Thanks in advance.

A: 

I am afraid you cannot do this. As far as what I have learned so far, you can rotate tho whole context, but not a single bitmap. Transformation matrix for what I know can only be applied to the whole canvas. (I am not a canvas guru, but I am doing extensive research on your same exact question)

PippoFlash.com
Hi, Thanks for the reply. Can't we rotate the bitmap around its center of axis using rotate animation.Or else. I better to go for Frame by Frame animation to get the desired rotation animation feel.Thanks in advance.
Andhravaala
+1  A: 

You can rotate around the centre axis:

Matrix matrix = new Matrix();

//move image

matrix.setTranslate(getXPos() - (imageWidth / 2), getYPos() - (imageHeight / 2));

//rotate image, getXPos, getYPos are x & y coords of the image

matrix.postRotate(angleInDegrees, getXPos() - imageWidth / 2, getYPos() - imageHeight / 2);

//rotatedBMP is the image you are drawing, 

canvas.drawBitmap(rotatedBMP, matrix, Paint);
Matt