我正在努力围绕其中心绘制一个旋转位图,并在不调整位图大小的情况下进行旋转。我正在通过游戏线程将我所有的 Sprite 绘制到屏幕上,所以我正在寻找一种包含原始位图而不是 Canvas 的解决方案。
提前致谢。
到目前为止,这是我的代码,它围绕其中心旋转位图,但调整它的大小。
i = i + 2;
transform.postRotate(i, Assets.scoresScreen_LevelStar.getWidth()/2, Assets.scoresScreen_LevelStar.getHeight()/2);
Bitmap resizedBitmap = Bitmap.createBitmap(Assets.scoresScreen_LevelStar, 0, 0, Assets.scoresScreen_LevelStar.getWidth(), Assets.scoresScreen_LevelStar.getHeight(), transform, true);
game.getGraphics().getCanvasGameScreen().drawBitmap(resizedBitmap, null, this.levelStar.getHolderPolygons().get(0), null);
更新:
我注意到这并不像听起来那么容易。我的旋转代码不是问题。位图旋转,但 dst rect 也必须根据旋转角度增加/减少,否则 bimap 会显得更小,因为它被绘制到固定的 dst rect 中。 所以我猜我必须开发一些方法来返回 dst 矩形。 因此需要在不显示调整大小的情况下旋转位图的方法:
public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) // I've got this method working
和
public static Rect rotateRect(Rect currentDst, int rotation) // Don't got this
我知道这需要一些数学知识(三角函数),有人愿意接受挑战吗? :P
最佳答案
您应该使用 Matrix
类绘制位图。下面是一个非常基本的想法,假设您想在“Ship”类中旋转图像。您在更新方法中更新当前位置矩阵。在 onDraw() 中,您使用新更新的位置矩阵绘制位图。这将绘制旋转的位图而不调整它的大小。
public class Ship extends View {
private float x, y;
private int rotation;
private Matrix position;
private Bitmap bitmap;
...
@Override
public void onDraw(Canvas canvas) {
// Draw the Bitmap using the current position
canvas.drawBitmap(bitmap, position, null);
}
public void update() {
// Generate a new matrix based off of the current rotation and x and y coordinates.
Matrix m = new Matrix();
m.postRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2);
m.postTranslate(x, y);
// Set the current position to the updated rotation
position.set(m);
rotation += 2;
}
....
}
希望对您有所帮助!
另请记住,在游戏循环中生成新的 Bitmap
对象会占用大量资源。
关于java - Android 围绕中心旋转位图而不调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11192757/