Android中的旋转位图问题

Android中的旋转位图问题,android,bitmap,rotation,Android,Bitmap,Rotation,我在正确旋转位图时遇到问题。我有一个SurfaceView,上面有多个位图。这些位图存在于arraylist中,并使用for循环,我为onDraw方法中的每个位图调用canvas.drawBitmap @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.BLACK); for (int i = 0; i < jumper.size

我在正确旋转位图时遇到问题。我有一个SurfaceView,上面有多个位图。这些位图存在于arraylist中,并使用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);
    }
}
整数方向为+1或-1,具体取决于手指拖动的方向。因此,对于每个MotionEvent.ACTION\u MOVE事件,图像应旋转1度

以下是问题:

图像不会围绕图像中心旋转。CW旋转以左下角为中心。逆时针旋转以右上角为中心。 由于图像不围绕中心旋转,因此图像在其初始边界之外旋转,并最终消失。 图像旋转时会变得模糊。 如果你能给我任何帮助,我将不胜感激


谢谢

请原谅我的回答有些离题,但你的循环引起了我的注意。可以用“更可读”的格式编写

for (YourJumperItem item : jumper) {
    canvas.drawBitmap(
        item.getGraphic(), item.getCoordinates().getX(),
        item.getCoordinates().getY(), null );
}

其中,YourJumperItem是跳线数组包含的类类型。不幸的是,我对旋转位图没什么好说的,我只是在推广这种编写for循环的简便方法。

使用矩阵将现有位图绘制到画布上,而不是创建新位图:

@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).getMatrix(), null);
    }
}

private void rotateJumper(int direction) {
    Matrix matrix = jumper.get(selectedJumperPos).getMatrix();
    if(matrix == null) {
        matrix = new Matrix();
        matrix.setTranslate(jumper.get(...).getCoord...().getX(), jumper.get(..).getCoord...().getY());
        jumper.get(selectedJumperPos).setMatrix(matrix);
    }
    Bitmap source = jumper.get(selectedJumperPos).getGraphic();
    matrix.postRotate(direction, source.getWidth() / 2, 
            source.getHeight() / 2);

}

这起作用了。进行了一次相当大的重新设计,但成功了。谢谢不是每次旋转时都创建新位图,而是在渲染期间保留和使用矩阵。在RotateCumper中,我们仅在需要时初始化矩阵,然后围绕位图的中心旋转矩阵。在绘制位图时,我们使用onDraw方法中的矩阵-矩阵可以包括在画布上绘制位图的位置、平移、绘制比例和旋转的数据。
@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).getMatrix(), null);
    }
}

private void rotateJumper(int direction) {
    Matrix matrix = jumper.get(selectedJumperPos).getMatrix();
    if(matrix == null) {
        matrix = new Matrix();
        matrix.setTranslate(jumper.get(...).getCoord...().getX(), jumper.get(..).getCoord...().getY());
        jumper.get(selectedJumperPos).setMatrix(matrix);
    }
    Bitmap source = jumper.get(selectedJumperPos).getGraphic();
    matrix.postRotate(direction, source.getWidth() / 2, 
            source.getHeight() / 2);

}