如何在不创建新位图的情况下旋转位图?

时间:2012-09-21 11:46:36

标签: java android image bitmap rotation

我正在使用此功能从已存在的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()); 重新绘制相同的可变图像:

{{1}}

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

1 个答案:

答案 0 :(得分:0)

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

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.