java Android 围绕中心旋转位图而不调整大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11192757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android rotate bitmap around center without resizing
提问by Luke Taylor
I'm struggling to draw a rotating bitmap around its center and do the rotating without resizing the bitmap. I'm drawing all my sprites to the screen via a game thread, so I'm looking for a solution that incorporates the original bitmap and not the canvas.
我正在努力围绕其中心绘制旋转位图,并在不调整位图大小的情况下进行旋转。我正在通过游戏线程将所有精灵绘制到屏幕上,因此我正在寻找一种包含原始位图而不是画布的解决方案。
Thanks in advance.
提前致谢。
This is my code so far, it turns a bitmap around its center, yet resizes it.
到目前为止,这是我的代码,它围绕其中心旋转位图,但会调整其大小。
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);
Update:
更新:
I've noticed this isn't as easy as it sounds. My rotating code is not the problem. The bitmap rotates, yet the dst rect will also have to increase/decrease depending on the angle of rotation, or else the bimap will appear smaller, since it's drawn into a fixed dst rect. So I'm guessing I'll have to develop some method that will return a dst rect. So the methods needed to rotate a bitmap without appeared resizing:
我注意到这并不像听起来那么容易。我的旋转代码不是问题。位图旋转,但 dst 矩形也必须根据旋转角度增加/减少,否则 bimap 会显得更小,因为它被绘制到固定的 dst 矩形中。所以我猜我必须开发一些方法来返回一个 dst 矩形。 所以需要旋转位图而不出现调整大小的方法:
public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) // I've got this method working
and
和
public static Rect rotateRect(Rect currentDst, int rotation) // Don't got this
I understand this will require some math (trig), anyone up for the challenge? :P
我知道这需要一些数学知识(trig),有人愿意接受挑战吗?:P
采纳答案by Luke Taylor
This worked for me!
这对我有用!
I created a method that returns a matrix. The matrix can be used in the following drawing method:
我创建了一个返回矩阵的方法。矩阵可用于以下绘制方法:
public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)
Here you go! (The parameter shape can be replaced with ease, if you would like that, just leave a comment):
干得好!(参数形状可以轻松替换,如果您愿意,只需发表评论):
public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {
float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();
Matrix rotateMatrix = new Matrix();
rotateMatrix.postScale(scaleWidth, scaleHeight);
rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
rotateMatrix.postTranslate(shape.getX(), shape.getY());
return rotateMatrix;
}
NB: If you want an animated rotation, the rotation parameter will have to be updated with new values every frame eg. 1 then 2 then 3 ...
注意:如果您想要动画旋转,则必须在每帧中使用新值更新旋转参数,例如。1然后2然后3 ...
回答by DRiFTy
You should draw the bitmap using the Matrix
class. Below is a very basic idea assuming that you want to rotate an image inside of a "Ship" class. You update the current position Matrix inside the update method. In the onDraw() you draw the bitmap using the newly updated position Matrix. This will draw the rotated bitmap without resizing it.
您应该使用Matrix
该类绘制位图。下面是一个非常基本的想法,假设您想在“船舶”类中旋转图像。您在 update 方法中更新当前位置 Matrix。在 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;
}
....
}
Hope that helps!
希望有帮助!
Also keep in mind that generating a new Bitmap
object inside you game loop will be resource intensive.
另请记住,Bitmap
在游戏循环中生成新对象将占用大量资源。