How to rotate a specific image between multiple images drawn on a canvas in android?

I need a little help when rotating one image around its axis axis among several images that stretch to the canvas in android.

I upload images to the canvas as shown below.

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

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

Thanks in advance.

+3
source share
3 answers

You can rotate around the central 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);
+6
source
//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 wrote from the link to the code above, works absolutely fine.

.

+1

I'm afraid you cannot do this. As far as I have learned so far, you can rotate the entire context, but not one bitmap. The transformation matrix for what I know can only be applied to the entire canvas. (I am not a dense caterpillar, but I am doing extensive research on the same exact issue)

0
source

Source: https://habr.com/ru/post/1750241/


All Articles