2016-03-04 218 views
0

我有一个位图从相机,调整大小后,它改变为水平,我需要旋转90度,但大多数the sample都使用矩阵来旋转,但是当我新的矩阵,它表示矩阵已过时,比我尝试用帆布,下面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); 
+0

矩阵不会被弃用,据我所知'012BGmap(resizePhoto,null,null); – Bhargav

+0

;'你正在使用null'Matrix'导致崩溃 – pskink

+0

@Bhargav http://developer.android.com/ intl/zh-tw/reference/android/opengl/Matrix.html –

回答

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

谢谢!我是noob,我混淆了android.graphics.Matrix,而不是android.opengl.Matrix –

+0

它发生。不要打扰你自己。我们都在不断学习! – Hazonko

+0

确保您使用画布上的不同位图。这会导致你的崩溃。 – Simon

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度旋转。我忘了。