一. BitmapFactory.Options 中inSampleSize的取值问题

关于inSampleSize的取值问题Google已经给出了一个推荐的算法:(https://developer.android.com/topic/performance/graphics/load-bitmap)

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2;
final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
} return inSampleSize;
}

实际测试下还好,基本可以满足不OOM的需求,但是隐隐觉得当inSampleSize的值很大的时候,图片的压缩质量会不会太严重?比如说App内中的相册功能,采用这个算法放大的时候,图片会不会变得很不清楚?一时也没有找到特别好的压缩算法。

二. 图片的放置问题

按照Google给出的示例代码,使用inSampleSize前要先把 options.inJustDecodeBounds = true; 设置成true:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

然后再计算出inSampleSize,最后再进行decode,个人觉得这段代码还是有缺陷,拿我的手机来说吧,分辨率是1440*2560, 需要显示的ImageView宽度是1440*810,需要加载的图片是1920*1080,通过上面Google的算法,算出inSampleSize的值为1,我把图片放置到xxxhdpi和xxhdpi的时候图片可以正常显示,但是我把图片放到mdpi和raw文件夹下的时候,程序一运行就Crash了,因为inSampleSize的默认值就是1,等于没有做任何处理,但是系统在加载时却会对图片再进行放大,一旦需要申请的字节数过多,系统就直接Crash了:

java.lang.RuntimeException: Canvas: trying to draw too large(132710400bytes) bitmap.

所以我觉得为了保险起见,最好在上面的代码中加上这么一句: options.inScaled = false;

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
   options.inScaled = false;
return BitmapFactory.decodeResource(res, resId, options);
}

保证不管我放置在任何文件夹,图片都不会被缩放。

最新文章

  1. 关于 REST
  2. Robot Framework--12 RFS+AutoItLibrary测试web对话框
  3. 删除某一BSC在某一时间段内的数据
  4. Restful 支持 自定义序列化
  5. 缓存一致性(Cache Coherency)入门
  6. js回调函数callback()
  7. [转] Boost智能指针——scoped_ptr
  8. C++自定义命名空间
  9. 不想作死系列--win7远程linux桌面之vncserver
  10. 【LaTeX排版】LaTeX论文排版<四>
  11. Mina框架(实战详解)
  12. PHP异步请求
  13. 微软开源自动机器学习工具NNI安装与使用
  14. JAVAjdk第一次作业
  15. JavaWeb——<c:forEach varStatus="status">
  16. mysql中count的注意事项
  17. 猪年设计素材:一波免费猪猪icon已为你备好
  18. 4.3 if-else语句使用
  19. 20171126--fragment的小项目
  20. C# 通过反射获取MVC Controller里的类名,方法名,参数列表,返回值类型,Description描述,自定义Attribute

热门文章

  1. DNS服务——域名解析转发 和 条件转发
  2. window kvm 虚拟机的创建
  3. 更改和配置本地yum源
  4. 电池管理系统(BMS)
  5. 0018SpringBoot连接docker中的mysql并使用druid数据源
  6. 23 export default和export的使用方式
  7. 前端面试题-JavaScript
  8. navigator对象及属性(userAgent)(扩展)
  9. win32通用控件
  10. P3038 [USACO11DEC]牧草种植Grass Planting