图片加载,避免oom篇(1)
来源:互联网 发布:美国警察知乎 编辑:程序博客网 时间:2024/06/16 05:43
根据所给的uri设置图片:思路是根据uri构建IO流,读取的时候用BitmapFactory的decode系列方法,并用options承载减压后构建新的bitmap对象,最后加载到ImageView控件中。 具体如下: public Bitmap decodeBitmapFromUri(Uri uri, int reqWidth, int reqHeight) { try { // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);// BitmapFactory.decodeResource(res, resId, options); // 调用上面定义的方法计算inSampleSize值 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // 使用获取到的inSampleSize值再次解析图片 options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } public 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 ? heightRatio : widthRatio; } return inSampleSize; }最后在加载图片的地方调用Bitmap bitmap = decodeBitmapFromUri(Uri.parse(photoUri), 100, 60);if (bitmap != null) ivPhoto.setImageBitmap(bitmap);方便的话,可以写一个ImageLoadUtils,并将他们声明为static,这样就可以随心所欲的调用该类的方法了!
1 0
- 图片加载,避免oom篇(1)
- 图片加载,避免OOM
- 如何避免图片加载OOM
- 高效加载图片,避免OOM
- Android避免加载图片出现OOM
- Android加载图片,避免OOM的解决方案
- 【Android应用】加载图片避免oom
- 图片加载避免OOM+代码示例
- Android 大量图片加载,使用什么加载库,避免OOM
- android 加载图片轻松避免OOM(out of memory)
- android 加载图片轻松避免OOM(out of memory)
- android 加载图片轻松避免OOM(out of memory)
- Android高效加载图片,有效避免程序OOM
- 图片处理,如何避免大图片加载的OOM
- Android高效加载图片,有效避免程序OOM
- Android高效加载图片,有效避免程序OOM
- Android高效加载图片,有效避免程序OOM
- 图片压缩和缓存高效加载避免oom
- 2017学习记录,一起进步
- js 中json对象转字符串
- SDK环境变量设置
- POJ 2559 Largest Rectangle in a Histogram 单调栈学习
- 如何建立一个基于事件驱动的全自动化交易系统
- 图片加载,避免oom篇(1)
- telnet如何岁memcached进行操作。
- bzoj 1927
- OpenGL 显示DICOM医学图像
- Webpack、Browserify和Gulp
- 网络框架的简单封装
- 【Android】ListView、RecyclerView、ScrollView里嵌套ListView 相对优雅的解决方案:NestFullListView
- 【转】并发场景下的中断处理
- 骰子点数识别之图像分割