ImageLoader简介和使用方法
1.功能概要
Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。
(1).使用多线程加载图片
(2).灵活配置ImageLoader的基本参数,包括线程数、缓存方式、图片显示选项等;
(3).图片异步加载缓存机制,包括内存缓存及SDCard缓存;
(4).采用监听器监听图片加载过程及相应事件的处理;
(5).配置加载的图片显示选项,比如图片的圆角处理及渐变动画。
2.简单实现
ImageLoader采用单例设计模式,ImageLoader imageLoader = ImageLoader.getInstance();得到该对象,每个ImageLoader采用单例设计模式,ImageLoader必须调用init()方法完成初始化。
- // 1.完成ImageLoaderConfiguration的配置
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
- .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
- .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
- .taskExecutor(...)
- .taskExecutorForCachedImages(...)
- .threadPoolSize(3) // default
- .threadPriority(Thread.NORM_PRIORITY - 1) // default
- .tasksProcessingOrder(QueueProcessingType.FIFO) // default
- .denyCacheImageMultipleSizesInMemory()
- .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
- .memoryCacheSize(2 * 1024 * 1024)
- .memoryCacheSizePercentage(13) // default
- .discCache(new UnlimitedDiscCache(cacheDir))// default
- .discCacheSize(50 * 1024 * 1024) // 缓冲大小
- .discCacheFileCount(100) // 缓冲文件数目
- .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
- .imageDownloader(new BaseImageDownloader(context)) // default
- .imageDecoder(new BaseImageDecoder()) // default
- .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
- .writeDebugLogs()
- .build();
- // 2.单例ImageLoader类的初始化
- ImageLoader imageLoader = ImageLoader.getInstance();
- imageLoader.init(config);
- // 3.DisplayImageOptions实例对象的配置
- // 以下的设置再调用displayImage()有效,使用loadImage()无效
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- .showStubImage(R.drawable.ic_stub) // image在加载过程中,显示的图片
- .showImageForEmptyUri(R.drawable.ic_empty) // empty URI时显示的图片
- .showImageOnFail(R.drawable.ic_error) // 不是图片文件 显示图片
- .resetViewBeforeLoading(false) // default
- .delayBeforeLoading(1000)
- .cacheInMemory(false) // default 不缓存至内存
- .cacheOnDisc(false) // default 不缓存至手机SDCard
- .preProcessor(...)
- .postProcessor(...)
- .extraForDownloader(...)
- .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)// default
- .bitmapConfig(Bitmap.Config.ARGB_8888) // default
- .decodingOptions(...)
- .displayer(new SimpleBitmapDisplayer()) // default 可以设置动画,比如圆角或者渐变
- .handler(new Handler()) // default
- .build();
- // 4图片加载
- // 4.1 调用displayImage
- imageLoader.displayImage(
- uri, /*
- String imageUri = "http://site.com/image.png"; // from Web
- String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
- String imageUri = "content://media/external/audio/albumart/13"; // from content provider
- String imageUri = "assets://image.png"; // from assets
- */
- imageView, // 对应的imageView控件
- options); // 与之对应的image显示方式选项
- // 4.2 调用loadImage
- // 对于部分DisplayImageOptions对象的设置不起作用
- imageLoader.loadImage(
- uri,
- options,
- new MyImageListener()); //ImageLoadingListener
- class MyImageListener extends SimpleImageLoadingListener{
- @Override
- public void onLoadingStarted(String imageUri, View view) {
- imageView.setImageResource(R.drawable.loading);
- super.onLoadingStarted(imageUri, view);
- }
- @Override
- public void onLoadingFailed(String imageUri, View view,
- FailReason failReason) {
- imageView.setImageResource(R.drawable.no_pic);
- super.onLoadingFailed(imageUri, view, failReason);
- }
- @Override
- public void onLoadingComplete(String imageUri, View view,
- Bitmap loadedImage) {
- imageView.setImageBitmap(loadedImage);
- super.onLoadingComplete(imageUri, view, loadedImage);
- }
- @Override
- public void onLoadingCancelled(String imageUri, View view) {
- imageView.setImageResource(R.drawable.cancel);
- super.onLoadingCancelled(imageUri, view);
- }
- }
3.支持的Uri
- String imageUri = "http://site.com/image.png"; // from Web
- String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
- String imageUri = "content://media/external/audio/albumart/13"; // from content provider
- String imageUri = "assets://image.png"; // from assets
- String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
加载drawables下图片,可以通过ImageView.setImageResource(...) 而不是通过上面的ImageLoader.
4.缓冲至手机
默认不能保存缓存,必须通过下面的方式指定
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- ...
- .cacheInMemory(true)
- .cacheOnDisc(true)
- ...
- .build();
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
- ...
- .defaultDisplayImageOptions(options)
- ...
- .build();
- ImageLoader.getInstance().init(config); // Do it on Application start
- ImageLoader.getInstance().displayImage(imageUrl, imageView); /*
- 默认为defaultDisplayImageOptions设定的options对象,此处不用指定options对象 */
或者通过下面这种方式
- DisplayImageOptions options = new DisplayImageOptions.Builder()
- ...
- .cacheInMemory(true)
- .cacheOnDisc(true)
- ...
- .build();
- ImageLoader.getInstance().displayImage(imageUrl, imageView, options); //此处指定options对象
由于缓存需要在外设中写入数据,故需要添加下面的权限
- <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.
ImageLoader简介和使用方法的更多相关文章
- NetCat简介与使用方法
精品学习网考试频道小编应广大考生的需要,特为参加考试的考生策划了“NetCat简介与使用方法”专题等有关资料,供考生参考! 在入侵中它是最经典的工具之一 ,NetCat被所有的网络安全爱好者和研究者称 ...
- PHP Socket(套接字连接)扩展简介和使用方法
PHP socket扩展是基于流行的BSD sockets,实现了和socket通讯功能的底层接口,它可以和客户端一样当做一个socket服务器. 使用这些函数时请注意,虽然他们中有很多和C函数同名的 ...
- nc之一:NetCat简介与使用方法
精品学习网考试频道小编应广大考生的需要,特为参加考试的考生策划了“NetCat简介与使用方法”专题等有关资料,供考生参考! 在入侵中它是最经典的工具之一 ,NetCat被所有的网络安全爱好者和研究者称 ...
- git简介及使用方法
一.git简介及安装1.git简介 Git 是用于 Linux 内核开发的版本控制工具.与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持, ...
- Retrofit简介与使用方法(翻译)
简介 Retrofit 是一个Square开发的类型安全的REST安卓客户端请求库.这个库为网络认证.API请求以及用OkHttp发送网络请求提供了强大的框架.Retrofit库让从web api下载 ...
- JavaScript简介与使用方法
1.JavaScript简介 1.1.JavaScript简史 最初:网络通信很慢,网页上的数据要传送到数据库验证,然后再返回错误结果,找客观过程要等很久,于是,网景公司开发出一门新语言,当时Java ...
- SAP Web Service简介与配置方法
[版权声明]本文为博主原创文章,转载请在明显位置注明出处. 一. SAP Web Service简介 二. SAP Web Service配置准备工作 1. 通过RZ10配置服务器名称和其他参数 2. ...
- Afinal简介和使用方法
Afinal简介 Afinal 是一个android的sqlite orm 和 ioc 框架.同时封装了android中的http框架,使其更加简单易用: 使用finalBitmap,无需考虑bitm ...
- xUtils简介和使用方法
xUtils简介 xUtils 包含了很多实用的android工具. xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10种谓词) ...
随机推荐
- pytest封神之路第三步 精通fixture
首先放一句"狠话". 如果你不会fixture,那么你最好别说自己会pytest. (只是为了烘托主题哈,手上的砖头可以放下了,手动滑稽) fixture是什么 看看源码 def ...
- flask-session、数据库连接池
flask-session 作用:将默认保存的签名cookie中的值保存到redis/memcached/file/Mongodb/SQLAlchemy 安装:pip3 install flask-s ...
- Nodejs-原型链污染
原型链污染 javascript 原型链 在javascript中,继承的整个过程就称为该类的原型链. 每个对象的都有一个指向他的原型(prototype)的内部链接,这个原型对象又有它自己的原型,一 ...
- Cypress系列(53)- as() 命令详解
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 作用 起别名以供以后使用 可在 cy.g ...
- eureka源码--服务的注册、服务续约、服务发现、服务下线、服务剔除、定时任务以及自定义注册中心的思路
微服务注册后,在注册中心的注册表结构是一个map: ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>& ...
- Python-集合 字典-set dict fronzenset
集合 set 1. 无序 2. 去重 3. 定义空集 set() numbers = {1, 3, 4, 5, 6, 5, 4, 4, 7, 8} print(numbers) print(numbe ...
- Go-归档文件-tar
文件归档 tar 1. 创建一个tar头部并自动填充tar头部信息 tar.FileInfoHeader() 联合 os.Stat() 方法 2. 手动填写 tar头部信息 tar.Header{} ...
- Java知识系统回顾整理01基础03变量03字面值
一.字面值定义 创建一个Hero对象会用到new关键字,但是给一个基本类型变量赋值却不是用new. 因为基本类型是Java语言里的一种内置的特殊数据类型,并不是某个类的对象. 给基本类型的变量赋值的 ...
- c++中的GetModuleFileName函数的用法以及作用
参考: 1. http://blog.sina.com.cn/s/blog_b078a1cb0101fw48.html 2. https://www.cnblogs.com/Satu/p/820393 ...
- Trie树【字典树】浅谈
最近随洛谷日报看了一下Trie树,来写一篇学习笔记. Trie树:支持字符串前缀查询等(目前我就学了这些qwq) 一般题型就是给定一个模式串,几个文本串,询问能够匹配前缀的文本串数量. 首先,来定义下 ...