Android Bitmap图片两种方案旋转90度的方法代码
程序员文章站
2022-01-13 17:51:20
...
今天小编就为大家分享一篇Android开发图片水平旋转90度方法,角度可以自定义,这里的90只是举例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。Android两种 旋转Bitmap方法,方法1. 利用Bitmap.createBitmap和方法2. 利用Canvas.drawBitmap。
方法1:
Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) { Matrix m = new Matrix(); m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2); try { Bitmap bm1 = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), m, true); return bm1; } catch (OutOfMemoryError ex) { } return null; }
方法2:
Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) { Matrix m = new Matrix(); m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2); float targetX, targetY; if (orientationDegree == 90) { targetX = bm.getHeight(); targetY = 0; } else { targetX = bm.getHeight(); targetY = bm.getWidth(); } final float[] values = new float[9]; m.getValues(values); float x1 = values[Matrix.MTRANS_X]; float y1 = values[Matrix.MTRANS_Y]; m.postTranslate(targetX - x1, targetY - y1); Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888); Paint paint = new Paint(); Canvas canvas = new Canvas(bm1); canvas.drawBitmap(bm, m, paint); return bm1; }
性能测试手机,CPU : MTK6575 ,1G Hz、MEM : 512MB、OS : andoid 2.3.7,图片尺寸1632 * 1224,结果:方法1在280 - 350毫秒间, 方法2在110毫秒左右。方法2优于方法1