图像在旋转时会改变大小。我该如何阻止这个?
我正在为 Android 制作游戏,我需要旋转图像。当我旋转它时,它的尺寸显然会发生变化。例如,当它旋转 45 度时(它是正方形,但我希望它适用于任何矩形,因此它是一个更通用的解决方案),它的宽度和高度变成对角线的长度,比原来的长。经过一些代数之后,您可以算出比例因子是 sqrt(2)。但我知道旋转位图的唯一方法是使用矩阵。例如:
matrix.postRotate(degrees);
rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
使用此方法,位图的大小保持不变,因此为了适应图像内容中的旋转图像,必须缩小。这导致了我的问题。
我现在所拥有的应该可以工作,但是运行时却不起作用。可能是因为它过于复杂,但这里是:
float totalRotated = 0;
public void rotate(float degrees){
if(mBitmap != null){
float increment = (float)((mBitmap.getWidth()/45.0)*(Math.sqrt(2)-1));
totalRotated += degrees;
totalRotated -= (float)((int)totalRotated/360)*360;
matrix.reset();
matrix.setRotate(totalRotated);
rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
rotated = Bitmap.createScaledBitmap(rotated, (int)(mBitmap.getWidth()+(((Math.abs(Math.abs(((int)totalRotated%90)-45)-45)))*increment)), (int)(mBitmap.getHeight()+(((Math.abs(Math.abs(((int)totalRotated%90)-45)-45)))*increment)), true);
}
}
使用 Log.d 函数,我能够确定最后一个语句中设置的尺寸是我期望的尺寸,但图像不改变大小。由于这甚至不起作用,我需要一种更好的方法来做到这一点或一种方法来修复我的方法。另外我的方法只适用于正方形。那么,我该怎么做呢?
编辑: 我的方法确实有效,我只是没有调用 setBounds() 但这不是唯一的方法,这效率很低。
I'm making a game for Android and I need to rotate an image. When I rotate it obviously it's dimensions change. For example when it's rotated 45 degrees (it's square but I'd like this to work for any rectangle so it's a more general solution) it's width and height become the length of the diagonal, which is longer than the original. After some algebra you can work out that the scale factor is sqrt(2). But the only way I know of to rotate a bitmap is with a matrix. Ex:
matrix.postRotate(degrees);
rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
Using this method the size of the bitmap remains constant so to fit the rotated image in the content of the image must shrink. Which causes my problem.
What I have now should work but when run doesn't. Probably because it's overly complex, never the less, here it is:
float totalRotated = 0;
public void rotate(float degrees){
if(mBitmap != null){
float increment = (float)((mBitmap.getWidth()/45.0)*(Math.sqrt(2)-1));
totalRotated += degrees;
totalRotated -= (float)((int)totalRotated/360)*360;
matrix.reset();
matrix.setRotate(totalRotated);
rotated = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
rotated = Bitmap.createScaledBitmap(rotated, (int)(mBitmap.getWidth()+(((Math.abs(Math.abs(((int)totalRotated%90)-45)-45)))*increment)), (int)(mBitmap.getHeight()+(((Math.abs(Math.abs(((int)totalRotated%90)-45)-45)))*increment)), true);
}
}
Using the Log.d
function I was able to determine that the dimensions set in the last statement are what I expect them to be but the image doesn't change size. Since this doesn't even work, I need a better way to do this or a way to fix my method. Also my method only works for squares. So, how can I do this?
EDIT:
My method does work, I just didn't call setBounds()
This can't be the only way to do it though, this is so inefficient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚您在寻找什么,因此这里有一个基于您的函数,它尝试计算新位图的正确宽度和高度,并通过仅创建一个位图来进行旋转。
It's not clear what you're looking for, so here's a function based on yours that attempts to compute the proper width and height of the new bitmap and do the rotation by creating just a single bitmap.
我尝试了 Gabe 的解决方案,并得到了与 Ramesh 和 Regis 相同的错误。这对我有用:
I tried Gabe's solution and got the same errors that Ramesh and Regis got. This worked for me: