首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android:如何在中心点上旋转位图

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

Stack Overflow用户
提问于 2010-11-13 00:38:32
回答 7查看 132K关注 0票数 84

我已经寻找了一天多的时间来解决这个问题,但是没有任何帮助,甚至这里的答案也没有。文档也没有解释任何东西。

我只是想在另一个物体的方向上旋转一下。问题是位图不是围绕固定点旋转的,而是围绕位图(0,0)旋转的。

下面是我遇到问题的代码:

代码语言:javascript
复制
  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()中的浮点参数并不重要。有没有人能帮助我,把我推向正确的方向?:)

EN

回答 7

Stack Overflow用户

发布于 2010-11-29 10:52:14

我希望下面的代码序列能对您有所帮助:

代码语言:javascript
复制
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检查以下方法

代码语言:javascript
复制
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
        Matrix m, boolean filter)

这将解释它对旋转和平移的作用。

票数 101
EN

Stack Overflow用户

发布于 2010-12-11 19:10:37

现在我又回到了这个问题上,因为我们已经完成了游戏,我只是想把对我有用的东西贴出来。

这是旋转矩阵的方法:

代码语言:javascript
复制
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);

所以它很简单,但我发现奇怪的是,我不能让它在setRotatepostTranslate的后面工作。也许有些人知道为什么这不起作用?现在,所有位图都可以正常旋转,但位图质量并不是没有一些细微的下降:/

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

票数 25
EN

Stack Overflow用户

发布于 2010-11-30 11:43:57

您还可以使用RotateAnimation旋转ImageView

代码语言:javascript
复制
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);
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4166917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档