关于bitamap旋转图片全黑

网上搜了一下bitmap旋转方法,一般分为两种:

一、

Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
//        setBitmapBGColor(bm, Color.WHITE);
        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();

        canvas.drawBitmap(bm1, m, paint);


        return bm1;
    }

二、

 public static Bitmap RotateBitmap( Bitmap bitmap,int angle) {
        // 旋转图片 动作
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (resizedBitmap != bitmap && bitmap != null && !bitmap.isRecycled()) {
            try {
                bitmap.recycle();
            } catch (Exception e) {
            }
        }
        return resizedBitmap;
    }

但是我使用第一种方法的时候却发现旋转完后bitmap确是一张黑图,搜了一下可能是因为透明色变黑了,我也就没管它了,直接使用第二种方法是没问题的。

发布了14 篇原创文章 · 获赞 23 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_27378951/article/details/99626340