首页 > 移动平台 > 详细

Android两种旋转Bitmap方法比较

时间:2014-06-06 21:58:24      阅读:533      评论:0      收藏:0      [点我收藏+]

方法1. 利用Bitmap.createBitmap

bubuko.com,布布扣
    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) {
            ex.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
bubuko.com,布布扣

 

方法2. 利用Canvas.drawBitmap

 

bubuko.com,布布扣
    Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
        try {
            int width = bm.getWidth();
            int height = bm.getHeight();
            Matrix matrix = new Matrix();
            matrix.setRotate(orientationDegree, ( float ) width / 2, ( float ) height / 2);
            float targetX = 0;
            float targetY = 0;
            if (orientationDegree == 90 || orientationDegree == 270) {
                if (width > height) {
                    targetX = ( float ) height / 2 - ( float ) width / 2;
                    targetY = 0 - targetX;
                } else {
                    targetY = ( float ) width / 2 - ( float ) height / 2;
                    targetX = 0 - targetY;
                }
            }
            matrix.postTranslate(targetX, targetY);
            Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);

            Paint paint = new Paint();
            Canvas canvas = new Canvas(bm1);
            canvas.drawBitmap(bm, matrix, paint);

            return bm1;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
bubuko.com,布布扣

 

性能测试:

 1. 手机 

    CPU : MTK6575 ,1G Hz

    MEM : 512MB

    OS : andoid 2.3.7

 2.图片尺寸1632 * 1224

 

结果: 

 1. 方法1在280 - 350毫秒间, 方法2在110毫秒左右。 

 2. 方法2优于方法1

Android两种旋转Bitmap方法比较,布布扣,bubuko.com

Android两种旋转Bitmap方法比较

原文:http://www.cnblogs.com/nick-zhang/p/3765707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!