Android设置采样率高效加载图片
来源:互联网 发布:windows模块更新程序 编辑:程序博客网 时间:2024/05/30 04:13
使用BitmapFactory.Options中的inSampleSize(采样率)参数来缩放加载图片是提高加载效率的核心思想。
步骤为
- 将BitmapFactory.Options的inJustDecodeBounds参数设为ture来预加载图片。
- 从Options读取出图片的原始宽高值,对应outWidth和outHeight参数。
- 结合所需要显示的大小计算合适的采样率inSampleSize值。
- 将Options中的inJustDecodeBounds参数设置false,重新加载图片资源即可。
通过加载一张高清图来说明设置采样率的效果
准备一张高清图放到drawable目录下:
布局代码(一个ImageView+两个水平按钮)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/img" android:layout_width="400dp" android:layout_height="300dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载原图" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="使用预加载" /> </LinearLayout></LinearLayout>
- MainActivity代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener { ImageView mImg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImg = (ImageView) findViewById(R.id.img); findViewById(R.id.btn1).setOnClickListener(this); findViewById(R.id.btn2).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn1: //普通设置图片 mImg.setImageResource(R.drawable.mm); break; case R.id.btn2://重新计算采样率加载图片 mImg.setImageBitmap(BitmapOptionLoadUtil.decodeBitmapFromResource(getResources(), R.drawable.mm, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 300, getResources().getDisplayMetrics()))); break; } }}
- BitmapOptionLoadUtil代码(和上步所说的步骤一样)
public class BitmapOptionLoadUtil { /** * 缩放加载图片 * * @param res * @param resId * @param decodeWidth * @param decodeHeight * @return */ public static Bitmap decodeBitmapFromResource(Resources res, @IdRes int resId, int decodeWidth, int decodeHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //预加载 BitmapFactory.decodeResource(res, resId, options); final int imgWidth = options.outWidth; //要加载的图片的宽 final int imgHeight = options.outHeight;//要加载的图片的高 int inSampleSize = 1; if (imgWidth > decodeWidth || imgHeight > decodeHeight) { int halfWidth = imgWidth / 2; int halfHeight = imgHeight / 2; while ((halfWidth / inSampleSize) >= decodeWidth && (halfHeight / inSampleSize) >= decodeHeight) { inSampleSize *= 2; } } options.inJustDecodeBounds = false; options.inSampleSize = inSampleSize; return BitmapFactory.decodeResource(res, resId, options); }}
- 运行程序,点击加载原图
通过IDE可以看到加载原图后分配的内存高达36.76M!
2.重新运行程序,点击使用预加载
可以看到比直接加载原图分配的内存少了2倍还多。可见设置合理的采样率可以高效的加载图片。
阅读全文
0 0
- Android设置采样率高效加载图片
- android高效加载图片
- Android 高效加载图片
- Android高效加载大量图片
- android图片:高效加载大图
- Android图片使用采样率压缩
- Android 打造高效的图片加载框架
- Android-高效加载图片经验分享
- Android 图片如何高效加载与缓存
- 高效异步加载图片
- 图片高效加载
- 高效加载图片
- Bitmap高效加载图片
- 高效加载图片
- Bitmap 高效加载图片
- 高效加载图片
- Android高效加载图片,有效避免程序OOM
- Android高效加载图片,有效避免程序OOM
- POJ 3278 Catch That Cow (BFS)
- hdu 1695 莫比乌斯反演入门题
- JavaScript难点——变量提升和函数提升(一)
- 控制台版图书管理系统
- 一张图看懂开源许可协议,开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- Android设置采样率高效加载图片
- C#事件记录
- Unity3D中的坐标系总结
- python学习笔记(三)tuple(元组)
- Redis 学习笔记(三):主从复制、主从配置和性能测试
- JVM笔记整理(第7章 虚拟机类加载机制)
- 微服务架构下的数据一致性保证(三):补偿模式
- 前端学习资源(全面,剧多)
- 实现一个能找到栈的最小元素的min函数