【问题标题】:How to rotate Bitmap without creating a new one?如何在不创建新的情况下旋转位图?
【发布时间】:2012-09-13 20:11:47
【问题描述】:

我正在使用它从现有的一个旋转 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());

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

【问题讨论】:

标签: java android image bitmap rotation


【解决方案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.

【讨论】:

  • 它不是旋转位图本身,而是在视图内旋转绘制它。我想要的是旋转位图并保存它。
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多