我已经寻找了一天多的时间来解决这个问题,但是没有任何帮助,甚至这里的答案也没有。文档也没有解释任何东西。
我只是想在另一个物体的方向上旋转一下。问题是位图不是围绕固定点旋转的,而是围绕位图(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()
中的浮点参数并不重要。有没有人能帮助我,把我推向正确的方向?:)
发布于 2010-11-29 10:52:14
我希望下面的代码序列能对您有所帮助:
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)
这将解释它对旋转和平移的作用。
发布于 2010-12-11 19:10:37
现在我又回到了这个问题上,因为我们已经完成了游戏,我只是想把对我有用的东西贴出来。
这是旋转矩阵的方法:
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
的后面工作。也许有些人知道为什么这不起作用?现在,所有位图都可以正常旋转,但位图质量并不是没有一些细微的下降:/
无论如何,谢谢你的帮助!
发布于 2010-11-30 11:43:57
您还可以使用RotateAnimation
旋转ImageView
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);
https://stackoverflow.com/questions/4166917
复制相似问题