Android:如何以中心点旋转位图
我已经寻找了一天多的时间来寻找这个问题的解决方案,但没有任何帮助,即使是这里的答案。文档也没有解释任何事情。
我只是想朝另一个物体的方向旋转。问题在于位图不是围绕固定点旋转,而是围绕位图 (0,0) 旋转。
这是我遇到问题的代码:
Matrix mtx = new Matrix();
mtx.reset();
mtx.preTranslate(-centerX, -centerY);
mtx.setRotate((float)direction, -centerX, -centerY);
mtx.postTranslate(pivotX, pivotY);
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, spriteWidth, spriteHeight, mtx, true);
this.bitmap = rotatedBMP;
奇怪的部分是,我如何更改 pre
/postTranslate()
中的值以及 float 参数并不重要setRotation()
。有人可以帮助我并将我推向正确的方向吗? :)
I've been looking for over a day for a solution to this problem but nothing helps, even the answers here. Documentation doesn't explain anything too.
I am simply trying to get a rotation in the direction of another object. The problem is that the bitmap is not rotated around a fixed point, but rather around the bitmaps (0,0).
Here is the code I am having troubles with:
Matrix mtx = new Matrix();
mtx.reset();
mtx.preTranslate(-centerX, -centerY);
mtx.setRotate((float)direction, -centerX, -centerY);
mtx.postTranslate(pivotX, pivotY);
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, spriteWidth, spriteHeight, mtx, true);
this.bitmap = rotatedBMP;
The weird part is, it doesn't matter how I change the values within pre
/postTranslate()
and the float arguments in setRotation()
. Can someone please help and push me in the right direction? :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我希望以下代码序列对您有所帮助:
如果您检查
~frameworks\base\graphics\java\android\graphics\Bitmap.java
中的以下方法,这将解释它对旋转和翻译。
I hope the following sequence of code will help you:
If you check the following method from
~frameworks\base\graphics\java\android\graphics\Bitmap.java
this would explain what it does with rotation and translate.
编辑:优化代码。
从资源中获取 Bitmap:
Edited: optimized code.
To get Bitmap from resources:
现在我们正在完成游戏,我又回到了这个问题,我只是想发布对我有用的内容。
这是旋转矩阵的方法:(
this.getCenterX()
基本上是位图 X 位置 + 位图宽度 / 2)以及绘制位图的方法(通过 RenderManager 调用) Class):
canvas.drawBitmap(this.bitmap, this.matrix, null);
所以它很简单,但我觉得有点奇怪,我无法让它工作通过
setRotate
,然后是postTranslate
。也许有人知道为什么这不起作用?现在所有位图都可以正确旋转,但位图质量并非没有轻微下降:/无论如何,感谢您的帮助!
I came back to this problem now that we are finalizing the game and I just thought to post what worked for me.
This is the method for rotating the Matrix:
(
this.getCenterX()
is basically the bitmaps X position + the bitmaps width / 2)And the method for Drawing the bitmap (called via a
RenderManager
Class):canvas.drawBitmap(this.bitmap, this.matrix, null);
So it is prettey straight forward but I find it abit strange that I couldn't get it to work by
setRotate
followed bypostTranslate
. Maybe some knows why this doesn't work? Now all the bitmaps rotate properly but it is not without some minor decrease in bitmap quality :/Anyways, thanks for your help!
您还可以使用
RotateAnimation
旋转ImageView
:You can also rotate the
ImageView
using aRotateAnimation
:您可以使用类似以下内容:
You can use something like following:
看看谷歌的月球着陆器样本,那里的飞船图像是动态旋转的。
月球着陆器代码示例
Look at the sample from Google called Lunar Lander, the ship image there is rotated dynamically.
Lunar Lander code sample
我使用了这个配置,但仍然存在像素化问题:
I used this configurations and still have the problem of pixelization :