源码地址:https://github.com/nostra13/Android-Universal-Image-Loader
一、
主要功能:多线程下载图片
根据用户需求配置ImageLoader(线程池、下载器、内存存储策略,磁盘存储策略、图片显示配置)
图片下载过程可监听(下载结果和下载进度)
设置图片圆角
二、
简单流程图:
三、
如何使用Universal-Image-Loader(git上面有例子参考那个就ok了)
1、 从git上下载源项目 把library做为module导入自己的项目,并关联
2、 添加权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3、 在Application中配置并初始化设置该类(根据个人需要来配置)
ImageLoaderConfiguration.Builderconfig = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2); //设置线程低优先等级(不能抢资源)
config.denyCacheImageMultipleSizesInMemory(); //一张图片可以按不同规格存储
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024* 1024); //50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); //Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
4、图片的各种格式DisplayImageOptions 的设置
options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) .showImageForEmptyUri(R.drawable.ic_empty) .showImageOnFail(R.drawable.ic_error) .cacheInMemory(true) .cacheOnDisk(true) .considerExifParams(true) .displayer(new CircleBitmapDisplayer(Color.WHITE, 5)) .build();
5、 图片加载
ImageLoader.getInstance().displayImage(IMAGE_URLS[position], holder.image, options, animateFirstListener);
四、Universal-Image-Loader内存缓存
缓存方式主要有3种(源码里面注释说的很清楚)
采用强引用缓存
/** * A cache that holds strong references to a limited number of Bitmaps. Each time a Bitmap is accessed, it is moved to * the head of a queue. When a Bitmap is added to a full cache, the Bitmap at the end of that queue is evicted and may * become eligible for garbage collection.<br /> * <br /> * <b>NOTE:</b> This cache uses only strong references for stored Bitmaps. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @since 1.8.1 */ public class LruMemoryCache implements MemoryCache {
采用弱引用缓存
/** * Memory cache with {@linkplain WeakReference weak references} to {@linkplain android.graphics.Bitmap bitmaps}<br /> * <br /> * <b>NOTE:</b> This cache uses only weak references for stored Bitmaps. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @since 1.5.3 */ public class WeakMemoryCache extends BaseMemoryCache {
强引用和弱引用结合
/** * Limited {@link Bitmap bitmap} cache. Provides {@link Bitmap bitmaps} storing. Size of all stored bitmaps will not to * exceed size limit. When cache reaches limit size then cache clearing is processed by FIFO principle.<br /> * <br /> * <b>NOTE:</b> This cache uses strong and weak references for stored Bitmaps. Strong references - for limited count of * Bitmaps (depends on cache size), weak references - for all other cached Bitmaps. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @since 1.0.0 */ public class FIFOLimitedMemoryCache extends LimitedMemoryCache {
个人在做项目的时候没有使用上述缓存方式,而是使用android自带的LruCache替换了,主要是听说android倾向于回收上述缓存里面的东西(没测过,不知道是不是真的。。),还有LruCache毕竟是google的,很成熟了。
五、Universal-Image-Loader磁盘缓存
现在的版本使用了DiskLruCache
总结:Universal-Image-Loader提供了一个很好的图片下载、缓存于一身的框架,如果可以使用就直接使用,如因项目限制的话,可以参考它来自己封装一个库来实现该功能,最主要的还是学习了别人如何设计、实现的的,比自己想出来的好多了(虽然自己写的 功能上也可以用。。。)