1.功能概要

Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。

(1).使用多线程加载图片
(2).灵活配置ImageLoader的基本参数,包括线程数、缓存方式、图片显示选项等;
(3).图片异步加载缓存机制,包括内存缓存及SDCard缓存;
(4).采用监听器监听图片加载过程及相应事件的处理;
(5).配置加载的图片显示选项,比如图片的圆角处理及渐变动画。

2.简单实现

ImageLoader采用单例设计模式,ImageLoader imageLoader = ImageLoader.getInstance();得到该对象,每个ImageLoader采用单例设计模式,ImageLoader必须调用init()方法完成初始化。

  1. //  1.完成ImageLoaderConfiguration的配置
  2. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
  3. .memoryCacheExtraOptions(480, 800)          // default = device screen dimensions
  4. .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
  5. .taskExecutor(...)
  6. .taskExecutorForCachedImages(...)
  7. .threadPoolSize(3)                          // default
  8. .threadPriority(Thread.NORM_PRIORITY - 1)   // default
  9. .tasksProcessingOrder(QueueProcessingType.FIFO) // default
  10. .denyCacheImageMultipleSizesInMemory()
  11. .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
  12. .memoryCacheSize(2 * 1024 * 1024)
  13. .memoryCacheSizePercentage(13)              // default
  14. .discCache(new UnlimitedDiscCache(cacheDir))// default
  15. .discCacheSize(50 * 1024 * 1024)        // 缓冲大小
  16. .discCacheFileCount(100)                // 缓冲文件数目
  17. .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
  18. .imageDownloader(new BaseImageDownloader(context)) // default
  19. .imageDecoder(new BaseImageDecoder()) // default
  20. .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
  21. .writeDebugLogs()
  22. .build();
  23. //  2.单例ImageLoader类的初始化
  24. ImageLoader imageLoader = ImageLoader.getInstance();
  25. imageLoader.init(config);
  26. //  3.DisplayImageOptions实例对象的配置
  27. //      以下的设置再调用displayImage()有效,使用loadImage()无效
  28. DisplayImageOptions options = new DisplayImageOptions.Builder()
  29. .showStubImage(R.drawable.ic_stub)          // image在加载过程中,显示的图片
  30. .showImageForEmptyUri(R.drawable.ic_empty)  // empty URI时显示的图片
  31. .showImageOnFail(R.drawable.ic_error)       // 不是图片文件 显示图片
  32. .resetViewBeforeLoading(false)  // default
  33. .delayBeforeLoading(1000)
  34. .cacheInMemory(false)           // default 不缓存至内存
  35. .cacheOnDisc(false)             // default 不缓存至手机SDCard
  36. .preProcessor(...)
  37. .postProcessor(...)
  38. .extraForDownloader(...)
  39. .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)// default
  40. .bitmapConfig(Bitmap.Config.ARGB_8888)              // default
  41. .decodingOptions(...)
  42. .displayer(new SimpleBitmapDisplayer()) // default 可以设置动画,比如圆角或者渐变
  43. .handler(new Handler())                             // default
  44. .build();
  45. //  4图片加载
  46. //  4.1 调用displayImage
  47. imageLoader.displayImage(
  48. uri,        /*
  49. String imageUri = "http://site.com/image.png";      // from Web
  50. String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
  51. String imageUri = "content://media/external/audio/albumart/13"; // from content provider
  52. String imageUri = "assets://image.png";             // from assets
  53. */
  54. imageView,      // 对应的imageView控件
  55. options);       // 与之对应的image显示方式选项
  56. //  4.2 调用loadImage
  57. //      对于部分DisplayImageOptions对象的设置不起作用
  58. imageLoader.loadImage(
  59. uri,
  60. options,
  61. new MyImageListener()); //ImageLoadingListener
  62. class MyImageListener extends SimpleImageLoadingListener{
  63. @Override
  64. public void onLoadingStarted(String imageUri, View view) {
  65. imageView.setImageResource(R.drawable.loading);
  66. super.onLoadingStarted(imageUri, view);
  67. }
  68. @Override
  69. public void onLoadingFailed(String imageUri, View view,
  70. FailReason failReason) {
  71. imageView.setImageResource(R.drawable.no_pic);
  72. super.onLoadingFailed(imageUri, view, failReason);
  73. }
  74. @Override
  75. public void onLoadingComplete(String imageUri, View view,
  76. Bitmap loadedImage) {
  77. imageView.setImageBitmap(loadedImage);
  78. super.onLoadingComplete(imageUri, view, loadedImage);
  79. }
  80. @Override
  81. public void onLoadingCancelled(String imageUri, View view) {
  82. imageView.setImageResource(R.drawable.cancel);
  83. super.onLoadingCancelled(imageUri, view);
  84. }
  85. }

3.支持的Uri

  1. String imageUri = "http://site.com/image.png";      // from Web
  2. String imageUri = "file:///mnt/sdcard/image.png";   // from SD card
  3. String imageUri = "content://media/external/audio/albumart/13"; // from content provider
  4. String imageUri = "assets://image.png";             // from assets
  5. String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)

加载drawables下图片,可以通过ImageView.setImageResource(...) 而不是通过上面的ImageLoader.

4.缓冲至手机

默认不能保存缓存,必须通过下面的方式指定

  1. DisplayImageOptions options = new DisplayImageOptions.Builder()
  2. ...
  3. .cacheInMemory(true)
  4. .cacheOnDisc(true)
  5. ...
  6. .build();
  7. ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
  8. ...
  9. .defaultDisplayImageOptions(options)
  10. ...
  11. .build();
  12. ImageLoader.getInstance().init(config); // Do it on Application start
  13. ImageLoader.getInstance().displayImage(imageUrl, imageView);    /*
  14. 默认为defaultDisplayImageOptions设定的options对象,此处不用指定options对象 */

或者通过下面这种方式

  1. DisplayImageOptions options = new DisplayImageOptions.Builder()
  2. ...
  3. .cacheInMemory(true)
  4. .cacheOnDisc(true)
  5. ...
  6. .build();
  7. ImageLoader.getInstance().displayImage(imageUrl, imageView, options); //此处指定options对象

由于缓存需要在外设中写入数据,故需要添加下面的权限

  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

5.OutOfMemoryError

如果OutOfMemoryError错误很常见,可以通过下面的方式设置
(1).减少configuration中线程池的线程数目(.threadPoolSize(...)) 推荐为1 - 5
(2).display options通过.bitmapConfig(Bitmap.Config.RGB_565)设置. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.
(3).使用configuration的memoryCache(new WeakMemoryCache())方法 或者不调用.cacheInMemory()方法
(4).display options通过.imageScaleType(ImageScaleType.IN_SAMPLE_INT) 或者 .imageScaleType(ImageScaleType.EXACTLY)方法
(4).避免使用RoundedBitmapDisplayer,它创建了一个新的ARGB_8888 Bitmap对象

6.内存缓存管理

通过imageLoaderConfiguration.memoryCache([new LruMemoryCache(1)]))对手机内存缓存进行管理

LruMemoryCache

API >= 9默认.it is moved to the head of a queue.

FreqLimitedMemoryCache

当超过缓存大小后,删除最近频繁使用的bitmap

LRULimitedMemoryCache

API < 9 默认.当超过缓存大小后,删除最近使用的bitmap

FIFOLimitedMemoryCache

FIFO rule is used for deletion when cache size limit is exceeded

LargestLimitedMemoryCache

The largest bitmap is deleted when cache size limit is exceeded

WeakMemoryCache

Unlimited cache

7.SDcard缓存管理

通过imageLoaderConfiguration.discCache([new TotalSizeLimitedDiscCache()]))对SD卡缓存进行管理

UnlimitedDiscCache

default The fastest cache, doesn't limit cache size

TotalSizeLimitedDiscCache

Cache limited by total cache size. If cache size exceeds specified limit then file with themost oldest lastusage date will be deleted

FileCountLimitedDiscCache

Cache limited by file count. If file count in cache directory exceeds specified limit then file with the most oldest last usage date will be deleted.

LimitedAgeDiscCache

Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.

UnlimitedDiscCache is 30%-faster than other limited disc cache implementations.

更多3
1

Android_开源框架_AndroidUniversalImageLoader网络图片加载的更多相关文章

  1. 转 Android_开源框架_AndroidUniversalImageLoader网络图片加载

    转自:http://www.cnblogs.com/wanqieddy/p/3836485.html 1.功能概要 Android-Universal-Image-Loader是一个开源的UI组件程序 ...

  2. 55、Android网络图片 加载缓存处理库的使用

         先来一个普通的加载图片的方法. import android.annotation.SuppressLint; import android.app.Activity; import and ...

  3. Android之网络图片加载的5种基本方式

    学了这么久,最近有空把自己用到过的网络加载图片的方式总结了出来,与大家共享,希望对你们有帮助. 此博客包含Android 5种基本的加载网络图片方式,包括普通加载HttpURLConnection.H ...

  4. Maven SSH三大框架整合的加载流程

    <Maven精品教程视频\day02视频\03ssh配置文件加载过程.avi;> 此课程中讲 SSH三大框架整合的加载流程,还可以,初步接触的朋友可以听一听. < \day02视频\ ...

  5. python web开发之flask框架学习(2) 加载模版

    上次学习了flask的helloword项目的创建,这次来学习flask项目的模版加载: 第一步:创建一个flask项目 第二步:在项目目录的templates文件夹下创建一个html文件 第三步: ...

  6. android官方开源的高性能异步加载网络图片的Gridview例子

    这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...

  7. Android 网络图片加载之cude 框架

    偶然发现了这个框架,阿里图片加载用的这个框架.非常简单操作步骤. 1.首先下载软件包,直接搜Cube ImageLoader 这个. 2.加入jar文件 3.使用前的配置: public class ...

  8. android 开发 - 网络图片加载库 Fresco 的使用。

    概述 Fresco 是 facebook 的开源类库,它支持更有效的加载网络图片以及资源图片.它自带三级缓存功能,让图片显示更高效. 介绍 Fresco 是一个强大的图片加载组件. Fresco 中设 ...

  9. Android项目框架之图片加载框架的选择

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 从Android爆发以后,自定义的控件如EditTextWithDelete.ActionBar.P ...

随机推荐

  1. C# 每月第一天和最后一天

    //每月第一天 - DateTime.Now.Day); //每月最后一天 var endTime=DateTime.Now.AddDays(1 - DateTime.Now.Day).AddMont ...

  2. Windwos8.1下配置PHP环境

    一.     下载安装包: Apache2.2:http://mirrors.cnnic.cn/apache//httpd/binaries/win32/httpd-2.2.25-win32-x86- ...

  3. 转:vue2.0 keep-alive最佳实践

    转载至:https://segmentfault.com/a/1190000008123035 1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性 ...

  4. BZOJ 1305 dance跳舞(最大流+二分答案)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1305 解题思路:转自:https://blog.csdn.net/u012288458/ ...

  5. Java编程的逻辑 (27) - 剖析包装类 (中)

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  6. 使用Ocelot构建GateWay

    添加Nuget包:Ocelot 添加配置文件Ocelot.json 具体配置可以看另一篇Ocelot配置 Json配置文件主要包含两个根节点: ReRoutes:路由重定向配置 都是数组结构 可以配置 ...

  7. Windows下SVN服务器搭建方法整理(apache)

    http://skydream.iteye.com/blog/437959 http://www.cnblogs.com/liuke209/archive/2009/09/23/1572858.htm ...

  8. 022 StringTokenizer替换掉String的操作

    一:说明 1.说明 String的操作特别消耗内存,所以可以考虑优化. 二:程序 1.程序修改 这部分程序属于Mapper端的程序,稍微优化一下. 2.程序 //Mapper public stati ...

  9. oj提交时常见错误归纳

    Presentation Error: 常见的PE错误应该有以下的几种情况: 每行输出之后有空行 每两行输出之间有空行 一行中,每个输出数字(或字符串,等)之间有空格 一行中,每个输出数字(或字符串, ...

  10. Element-ui el-cascader不触发prop?

    html代码: <el-form label-position="right" label-width="100px" :model="form ...