简体   繁体   English

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

[英]How to rotate Bitmap without creating a new one?

I'm using this to get rotated Bitmap from existed one : 我正在使用它来从现有的Bitmap获取旋转的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);
    }

Is it possible to do it without creating new bitmap? 是否可以在不创建新位图的情况下完成?

I was trying to redraw the same mutable image with Canvas : 我试图用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());

but this approach had just resulted in corrupted bitmap. 但这种方法刚刚导致位图损坏。 So is there any possibilities to achieve it? 那么有没有可能实现它?

This is the simplest rotation on canvas code, without creating a new bitmap 这是画布代码上最简单的旋转,无需创建新的位图

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM