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

标签 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());

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

最佳答案

这是最简单的 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/

相关文章:

java - getArgument() 始终为 null

java.lang.NoClassDefFoundError 安卓工作室

Android PagerTabStrip 文本消失

Android:我可以在 Android Market 上发布使用 AmazonSDK 应用内支付的应用吗?还是必须使用 Amazon 的 App Store?

html - 如何使图像大小相同且响应迅速

javascript - 如何为图像框创建翻转?

java - Eclipse Code Formatter Xml 文档

java.io 困境

android - 制作自己的系统栏

php - 将图片上传到服务器并将图片路径存储在mysql数据库中