我需要在安卓中将多张图片绘制到画布上,并围绕其中一张图片的轴心旋转。以下是我向画布加载图片的代码:
canvas.drawBitmap(mMachineBackground, 0, 0, null);
canvas.drawBitmap(mMachineRotator, 0, 0, null);
我希望只旋转第二个位图,而不是旋转整个画布(包括第一个位图在内)。请围绕其轴心使第二个位图旋转,并保持第一个位图不变。谢谢。
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);
我根据上面的代码参考编写的这段代码完全正常运行。
我能够在画布上旋转特定的图像。
很抱歉,您不能这样做。 就我目前所学的知识而言,您可以旋转整个上下文,但不能旋转单个位图。据我所知,变换矩阵只能应用于整个画布。 (我不是画布大师,但我正在对您完全相同的问题进行广泛的研究)