我正在使用它从现有的 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());
但这种方法只会导致损坏的位图。 那么有没有可能实现呢?
最佳答案
这是最简单的 Canvas 旋转代码,无需创建新的位图
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.
关于java - 如何在不创建新位图的情况下旋转位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12529941/