我一直在寻找解决这个问题的方法,但没有任何帮助,即使是这里的答案。文档也没有解释任何内容。
我只是想在另一个对象的方向上旋转。问题是位图不是围绕固定点旋转,而是围绕位图(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()
中的值和 setRotation()< 中的浮点参数并不重要
。有人可以帮助并插入我朝着正确的方向前进吗? :)
最佳答案
希望下面的代码对你有所帮助:
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());
如果您从 ~frameworks\base\graphics\java\android\graphics\Bitmap.java 中检查以下方法
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
Matrix m, boolean filter)
这将解释它对旋转和平移的作用。
关于Android:如何在中心点旋转位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4166917/