7

我正在使用它来Bitmap从现有的旋转:

private Bitmap getRotatedBitmap(Bitmap bitmap, int angle) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(angle);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }

是否可以在不创建新位图的情况下做到这一点?

我试图用以下方法重绘相同的可变图像Canvas

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(targetBitmap, matrix, new Paint());

但这种方法刚刚导致位图损坏。那么有没有可能实现呢?

4

1 回答 1

1

这是画布代码上最简单的旋转,无需创建新的位图

canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (imageW / 2), Y + (imageH / 2)); //rotate the canvas
canvas.drawBitmap(imageBmp, X, Y, null); //draw the image on the rotated canvas
canvas.restore();  // restore the canvas position.
于 2012-09-21T13:52:39.473 回答