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

标签 android matrix bitmap center 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;

奇怪的是,我如何更改 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/

相关文章:

java - Edittext 专注于开始并防止推送布局

java - Android pei 的sinch视频通话摄像头权限问题(9)

android - 某些应用程序如何阻止/替换单挑通知?

android - android TV 中的 videoview 屏幕变黑

C++ 类转换运算符

r - 为什么矩阵到栅格显示旋转?

android - android中的位图静态

matlab - 使用 matlab 中结构变量的值构建矩阵

android - 解码位图返回 null

java - 无法从 base64 字符串中获取完整的图像质量