简体   繁体   中英

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.

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);
//Drawing the Player Canon.
           Matrix matrix = new Matrix();
           //move image
           newHeight = getHeight() - canon1[0].getHeight() - stand1.getHeight();
           Log.d(TAG, "New Height : " + newHeight);
           //matrix.setTranslate(0, getHeight() - canon1[0].getHeight() + stand1.getHeight());
           matrix.setTranslate(-newHeight,newHeight);

           //   rotate image, getXPos, getYPos are x & y coords of the image (ANgle in degree)).
           //matrix.postRotate(45, 0, getHeight() - canon1[0].getHeight() + stand1.getHeight());
           matrix.postRotate(-30,0,0);
           //Draw function. 
           canvas.drawBitmap(canon1[0], matrix, null);

This code which I had writen from the reference of the above code works absolutely fine.

I am able to rotate a particular image on the canvas.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM