【发布时间】:2011-11-09 22:19:52
【问题描述】:
我无法正确旋转位图。我有一个 SurfaceView,上面有多个位图。这些位图存在于数组列表中,并使用 for 循环为 onDraw 方法中的每个位图调用 canvas.drawBitmap。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
for (int i = 0; i < jumper.size(); i++) {
canvas.drawBitmap(jumper.get(i).getGraphic(), jumper.get(i)
.getCoordinates().getX(), jumper.get(i).getCoordinates()
.getY(), null);
}
}
我试图让用户选择一个特定的位图(许多位图之一),然后在用户在屏幕上拖动手指时让该位图旋转。所以这里是轮换代码。现在我只是使用默认的 android 图标 (72x72px) 存在于屏幕中心附近的某个随机位置。
private void rotateJumper(int direction) {
Matrix matrix = new Matrix();
Bitmap source = jumper.get(selectedJumperPos).getGraphic();
matrix.postRotate(direction, source.getWidth() / 2,
source.getHeight() / 2);
int x = 0;
int y = 0;
int width = 72;
int height = 72;
Bitmap tempBitmap = Bitmap.createBitmap(source, x, y, width, height,
matrix, true);
jumper.get(selectedJumperPos).setGraphic(tempBitmap);
}
整数方向是 +1 或 -1,具体取决于手指拖动的方向。所以图像应该为每个 MotionEvent.ACTION_MOVE 事件旋转 1 度。
以下是问题:
- 图像不围绕图像中心旋转。 CW 以左下角为中心旋转。 CCW 以右上角为中心旋转。
- 由于不是围绕中心旋转,图像会在初始范围之外旋转并最终消失。
- 图像在旋转时变得模糊。
您能给我的任何帮助将不胜感激。
谢谢!
【问题讨论】: