Android的位图旋转中心的周围无调整 [英] Android rotate bitmap around center without resizing
问题描述
我挣扎提请围绕其中心旋转的位图,并没有做调整位图的旋转。我画我所有的精灵通过游戏线程的屏幕,所以我在寻找一个解决方案,整合了原来的位图,而不是在画布上。
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.
在此先感谢。
这是我的code到目前为止,原来绕其中心位图,但调整它的大小。
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);
更新:
我发现这并不像听起来那么容易。我的旋转code是没有问题的。位图旋转,但DST的矩形也将不得不增加/减少取决于旋转的角度,否则bimap的会出现较小的,因为它绘制成一个固定的DST正确。 所以,我猜我会开发一些方法会返回一个DST正确。 所以方法需要旋转的位图没有出现调整:的
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:
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
I understand this will require some math (trig), anyone up for the challenge? :P
推荐答案
这为我工作!
我创建了一个返回一个矩阵的方法。基质可以在以下拉伸方法可以使用:
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;
}
注意:如果你想要一个动画旋转,旋转参数必须与新的价值观每一帧,例如更新。 1,然后2,然后3 ...
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 ...
这篇关于Android的位图旋转中心的周围无调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!