Awei Hsue
Awei Hsue

Reputation: 257

how to Rotate bitmap by canvas

I have a bitmap from camera , after resize , it change to horizontal , I need to rotate 90 degree , but most of the sample are use Matrix to rotate , but when I new Matrix , it said matrix is deprecated , than I try to use CANVAS , following this , first time to use it , trying to figure it out , but can not rotate it , app crash , help please

code

  resizePhoto = BitmapFactory.decodeFile(imageLocation,bmOption);

//  R o t a t e    B i t m a p   90 degree

    Canvas canvas = new Canvas(resizePhoto);
    canvas.rotate(90);
    canvas.drawBitmap(resizePhoto , null ,null);

Upvotes: 0

Views: 2157

Answers (2)

Simon
Simon

Reputation: 11190

You might want to rotate using a matrix passed into Bitmap.createBitmap. It should be faster than using a Canvas.

Matrix matrix = new Matrix();
matrix.setRotate(angle);
Bitmap resizePhoto = BitmapFactory.decodeFile(imageLocation, bmOption);
Bitmap rotatedPhoto = Bitmap.createBitmap(resizePhoto, 0, 0, 
    resizePhoto.getWidth(), resizePhoto.getHeight(), matrix, true);
resizePhoto.recycle();

You might need to swap the getWidth() and getHeight() around for an exact 90 degree rotation. I forget.

Upvotes: 0

Hazonko
Hazonko

Reputation: 1035

Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
yourCanvas.drawBitmap(yourBitmap, matrix, null);

Upvotes: 1

Related Questions