How to rotate particular image among multiple images drawn to canvas in android?
I need a small help on rota开发者_StackOverflow中文版ting 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)
精彩评论