如何通过画布旋转位图

时间:2016-03-04 10:21:08

标签: android canvas bitmap

我有一个来自相机的位图,调整大小后,它变为水平,我需要旋转90度,但大多数the sample都使用Matrix来旋转,但是当我新建Matrix时,它表示矩阵已被弃用,比我尝试使用CANVAS,在this之后,第一次使用它,试图解决它,但无法旋转它,应用程序崩溃,请帮助

代码

  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);

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可能希望使用传递到Bitmap.createBitmap的矩阵进行旋转。它应该比使用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();

您可能需要交换getWidth()getHeight()左右才能完全旋转90度。我忘了。

相关问题