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

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


我只是试图使旋转方向朝另一个对象的方向。问题在于位图不是围绕固定点旋转,而是围绕位图(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()以及中的float参数,都没有关系setRotation()。有人可以帮忙,把我推向正确的方向吗?:)


FFIVE
浏览 788回答 3
3回答

哔哔one

希望以下代码序列对您有所帮助: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.javapublic static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,        Matrix m, boolean filter)这将解释旋转和平移的作用。

幕布斯6054654

优化代码。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);}要从资源获取位图:Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);

墨色风雨

现在我们要完成游戏的设计,我回到了这个问题,我只是想发布对我有用的东西。这是旋转矩阵的方法:this.matrix.reset();this.matrix.setTranslate(this.floatXpos, this.floatYpos);this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY()); (this.getCenterX()基本上是位图X位置+位图宽度/ 2)以及绘制位图的方法(通过RenderManager类调用):canvas.drawBitmap(this.bitmap, this.matrix, null);因此直截了当是棘手的,但我感到有点奇怪,我无法让它setRotate紧随其后postTranslate。也许有人知道为什么这行不通?现在所有的位图都可以正确旋转,但是在位图质量上并没有降低一些:/无论如何,谢谢您的帮助!
打开App,查看更多内容
随时随地看视频慕课网APP