Home:ALL Converter>How to rotate Bitmap without creating a new one?

How to rotate Bitmap without creating a new one?

Ask Time:2012-09-21T19:46:36         Author:amukhachov

Json Formatter

I'm using this to get rotated Bitmap from existed one :

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:

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?

Author:amukhachov,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12529941/how-to-rotate-bitmap-without-creating-a-new-one
yy