Android的:如何旋转一个位图上的一个中心点中心点、个位、图上、Android

2023-09-11 12:25:06 作者:△ 花残。

我一直在寻找了一天解决这个问题,但没有什么帮助,即便是在这里的答案。文档不解释什么了。

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).

下面是code我有烦恼有:

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? :)

推荐答案

我希望code以下顺序将帮助您:

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());

如果您检查从下面的方法〜框架\基地\显卡\ java的\机器人\显卡\ Bitmap.java

If you check the following method from ~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)

这可以解释它做什么用的旋转和平移。

this would explain what it does with rotation and translate.