Android:如何在中心点上旋转位图

Ste*_*fan 83 android center bitmap matrix image-rotation

我一直在寻找解决这个问题的一天,但没有任何帮助,甚至这里的答案.文档也没有解释任何内容.

我只是想在另一个物体的方向上进行旋转.问题是位图不是围绕固定点旋转,而是围绕位图(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;
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如何更改pre/ postTranslate()和float参数中的值并不重要setRotation().有人可以帮助并推动我朝着正确的方向前进吗?:)

Sud*_*lan 101

我希望以下代码序列可以帮助您:

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());
Run Code Online (Sandbox Code Playgroud)

如果您检查以下方法 ~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)
Run Code Online (Sandbox Code Playgroud)

这可以解释它对旋转和翻译的作用.

  • 只是评论这个过程非常耗费内存,不应该在步骤方法中使用. (3认同)

Arv*_*vis 78

编辑:优化代码.

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Run Code Online (Sandbox Code Playgroud)

要从资源中获取位图:

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);
Run Code Online (Sandbox Code Playgroud)

  • 你确定这是有效的吗?如果RotateBitmap()处于执行N次的循环中,则根据位图的分辨率,您可能会浪费大量内存以及所有新的Matrix对象分配. (6认同)

小智 25

我现在回到这个问题,我们正在最终确定游戏,我只是想发布对我有用的东西.

这是旋转矩阵的方法:

this.matrix.reset();
this.matrix.setTranslate(this.floatXpos, this.floatYpos);
this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY()); 
Run Code Online (Sandbox Code Playgroud)

(this.getCenterX()基本上是位图X位置+位图宽度/ 2)

绘制位图的方法(通过RenderManager类调用):

canvas.drawBitmap(this.bitmap, this.matrix, null);

所以它是直截了当的说法,但我觉得很奇怪,我无法通过setRotate其后的工作postTranslate.也许有人知道为什么这不起作用?现在所有的位图都可以正常旋转,但是位图质量不会有一些小的下降:/

无论如何,谢谢你的帮助!


Mac*_*rse 7

您还可以ImageView使用以下方式旋转RotateAnimation:

RotateAnimation rotateAnimation = new RotateAnimation(from, to,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(ANIMATION_DURATION);
rotateAnimation.setFillAfter(true);

imageView.startAnimation(rotateAnimation);
Run Code Online (Sandbox Code Playgroud)

  • 你在回答错误的问题.他不想旋转视图,只需要在其中旋转一个位图. (4认同)

Sud*_*lan 5

您可以使用以下内容:


Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
RectF rectF = new RectF(0, 0, source.getWidth(), source.getHeight());
matrix.mapRect(rectF);
Bitmap targetBitmap = Bitmap.createBitmap(rectF.width(), rectF.height(), config);
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(source, matrix, new Paint());