Bitmap有效的压缩方式

来源:互联网 发布:android 数据库缓存 编辑:程序博客网 时间:2024/05/16 06:45

在Android开发中我们都会遇到在一个100*100的ImageView上显示一张过大的图片,如果直接把这张图片显示上去对我们应用没有一点好处反而存在OOM的危险,所以我们有必要采用一种有效压缩方式来显示上去。

private void calculateBitmapInSimpleSize() {        Bitmap _bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_homepage);        getBitmapDatas(_bitmap);        BitmapFactory.Options optioins = new BitmapFactory.Options();        optioins.inJustDecodeBounds = true;//        optioins.inPreferredConfig = Bitmap.Config.RGB_565;//11158560        optioins.inPreferredConfig = Bitmap.Config.ARGB_8888;//22317120        BitmapFactory.decodeResource(getResources(), R.drawable.bg_homepage, optioins);        int reqWidth = optioins.outWidth;        int reqHeight = optioins.outHeight;        Log.w(TAG, "reqWidth = " + reqWidth);        Log.w(TAG, "reqHeight = " + reqHeight);        int inSampleSize = 1;        final int widthRatio = Math.round((float)reqWidth / 100f);        final int heigthRatio = Math.round((float) reqHeight / 100f);        // 取最小值 这将保证压缩出来的图片大于或者等于请求的宽度或者高度        inSampleSize = widthRatio > heigthRatio ? heigthRatio : widthRatio;        Log.w(TAG, "first inSampleSize = " + inSampleSize);        final int totalPixel = 100 * 100;        final int totalReqPixel = reqWidth * reqHeight * 2;        Log.w(TAG, "totalReqPixel = " + totalReqPixel);        while (totalPixel / (inSampleSize * inSampleSize) > totalReqPixel) {            Log.w(TAG, "totalPixel = " + (totalPixel / (inSampleSize * inSampleSize)));            inSampleSize ++;        }        Log.w(TAG, "LastInSampleSize = " + inSampleSize);        optioins.inJustDecodeBounds = false;        Bitmap lastBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg_homepage, optioins);        getBitmapDatas(lastBitmap);        mImageView.setImageBitmap(lastBitmap);    }

通过打印log我们可以清楚发现一张原始的图片占有22317120字节,经过压缩后11158560(RGB_565)/ 22317120(RGB8888)明显所占用的内存都减少了,尽量降低这种情况带来的OOM。
做法:
1.optioins.inJustDecodeBounds = true设置为true可用于读取该bitmap的宽高且不会占用内存。
2.optioins.inPreferredConfig = Bitmap.Config.RGB_565设置在内存中以占用最少的方式,相比RGB_8888只有其一半的内存占有。
3.final int widthRatio = Math.round((float)reqWidth / 100f);
final int heigthRatio = Math.round((float) reqHeight / 100f);
inSampleSize = widthRatio > heigthRatio ? heigthRatio : widthRatio;
计算压缩比例,取最小值 这将保证压缩出来的图片大于或者等于请求的宽度或者高度。
4.在要显示到ImageView的时候optioins.inJustDecodeBounds = false设回false这样就能正常显示了

// 计算bitmap所占内存值 public void getBitmapDatas(Bitmap bitmap) {         Log.w(TAG, "Bitmap size = " + bitmap.getByteCount());    }

采用以上的压缩方式 我们就能避免一张过大的图片”浪费”的显示在ImageView上造成内存消耗过大。

1 0