Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate particular image among multiple images drawn to canvas in android?

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.

like image 255
Andhravaala Avatar asked Dec 27 '25 16:12

Andhravaala


1 Answers

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);
like image 140
Matt Avatar answered Dec 31 '25 08:12

Matt