如何在Android中将绘制到画布上的多个图像中旋转特定图像?

3

我需要在安卓中将多张图片绘制到画布上,并围绕其中一张图片的轴心旋转。以下是我向画布加载图片的代码:

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

我希望只旋转第二个位图,而不是旋转整个画布(包括第一个位图在内)。请围绕其轴心使第二个位图旋转,并保持第一个位图不变。谢谢。
3个回答

6
您可以围绕中心轴旋转:
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);

爱你哥们...我已经寻找了4天,最终你的答案给了我解决方案。非常感谢你... - Taruni

1
//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);

我根据上面的代码参考编写的这段代码完全正常运行。

我能够在画布上旋转特定的图像。


0

很抱歉,您不能这样做。 就我目前所学的知识而言,您可以旋转整个上下文,但不能旋转单个位图。据我所知,变换矩阵只能应用于整个画布。 (我不是画布大师,但我正在对您完全相同的问题进行广泛的研究)


嗨,谢谢回复。我们不能使用旋转动画围绕位图的轴心进行旋转吗? 否则,我最好选择逐帧动画来获得所需的旋转动画效果。 提前致谢。 - Andhravaala

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接