android使用Matrix进行图片旋转,空白区域如何透明

我在使用matrix对bitmap进行旋转的时候,图片到旋转成功了,但是空白区域是黑色的,要如何才能设置成透明色?
使用中发现JPG文件的图片出现以上情况,PNG旋转后空白区域是透明的。尝试在android端通过bitmap.compress将JPG转换成PNG在进行旋转,空白区域还是黑色的。
目前使用的方法是先通过decodeFromFile从文件获取bitmap,然后通过算法先计算出旋转后的图片尺寸,按新的尺寸新建一个bitmap。通过canvas绘制旋转后的图片。这样可以实现空白区域透明化,但是一旦图片尺寸过大或者旋转角度大了,就容易内存溢出!
求问该如何解决!

以下是我直接对bitmap进行matrix旋转代码
BitmapFactory.Options tempOpts = new BitmapFactory.Options();
tempOpts.inPurgeable = true;
final Bitmap bitmap = BitmapFactory.decodeStream(in, null, tempOpts);
Matrix matrix = new Matrix();
matrix.postRotate(45);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

加上tempOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;
试试呢

如果只是用于显示的话可以把bitmap的尺寸缩小一些设置一下,具体代码可以参照下面的试一下应该不会有内存溢出
BitmapFactory.Options tempOpts = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

BitmapFactory.decodeFile(filePath, options);//inJustDecodeBounds设置为true,在decodeFile时可以返回一个为null的bitmap,但是可以返回Bitmap的宽度、高度
tempOpts .inSampleSize = calculateInSampleSize(tempOpts , 480, 800);//按照480,800的大小,等比例缩小图片的大小
empOpts.inPurgeable = true;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, tempOpts );
Matrix matrix = new Matrix();
matrix.postRotate(45);
Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

计算缩放比例的方法calculateInSampleSize
int calculateInSampleSize(BitmapFactory.Options options,
                                         int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
    }
    return inSampleSize;
}