前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Bitmap压缩方式分析

Android Bitmap压缩方式分析

作者头像
砸漏
发布2020-10-22 11:24:58
5780
发布2020-10-22 11:24:58
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

Android Bitmap压缩方式分析

在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的手机拍照都能达到3M左右了,尺寸压缩一般可用于生成缩略图。

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

代码语言:javascript
复制
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这样就能正常显示了

代码语言:javascript
复制
// 计算bitmap所占内存值
 public void getBitmapDatas(Bitmap bitmap) {
     Log.w(TAG, "Bitmap size = " + bitmap.getByteCount());
  }

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档