Android两种 旋转Bitmap方法

原文地址:https://www.cnblogs.com/exmyth/p/4632700.html

旋转有两种方案,一种是旋转控件,还有一种是在旋转bitmap

旋转bitmap有两种方式

Android两种 旋转Bitmap方法

方法1. 利用Bitmap.createBitmap

   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. 利用Canvas.drawBitmap

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

方法2优于方法1

猜你喜欢

转载自blog.csdn.net/chenhuakang/article/details/82048630
今日推荐