Android:如何在中心点上旋转位图 [英] Android: How to rotate a bitmap on a center point
问题描述
我已经寻找了一天多来解决这个问题,但没有任何帮助,即使是这里的答案.文档也没有解释任何事情.
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.
我只是想在另一个对象的方向上旋转.问题是位图不是围绕固定点旋转,而是围绕位图 (0,0).
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;
奇怪的部分是,我如何更改 pre
/postTranslate()
中的值和 setRotation()<中的浮点参数并不重要/代码>.有人可以帮助并推动我朝着正确的方向前进吗?:)
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? :)
推荐答案
希望以下代码序列对您有所帮助:
I hope the following sequence of code will help you:
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());
如果您从~frameworksasegraphicsjavaandroidgraphicsBitmap.java
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
这将解释它对旋转和平移的作用.
this would explain what it does with rotation and translate.
这篇关于Android:如何在中心点上旋转位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!