Universal-Image-Loader 使用步骤
开源框架利与弊
开源框架给开发者提供了便利,避免了重复造轮子,但是却隐藏了一些开发上的细节,如果不关注其内部实现,那么将不利于开发人员掌握核心技术,当然也谈不上更好的使用它,计划分析项目的集成使用和低层实现。
基本四部曲
- imageLoaderConfiguration
- ->imageLoader.init(imageLoaderConfiguration)
- displayImageOptions
- ->imageLoader.displayImage(...,displayImageOptions,...)
1. ImageLoaderConfiguration(有默认)
通过ImageLoaderConfiguration进行配置
两种方式:
- 默认
ImageLoaderConfiguration configuration = ImageLoaderConfiguration
.createDefault(this);
- 详细的
File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(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
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build();
2.给ImageLoader配置上一步信息
ImageLoader.getInstance().init(configuration);
ImageLoader使用单例模式:源码
public static ImageLoader getInstance(){
/*为了线程安全加了锁
*一但ImageLoader对象有了,就直接跳过同步
*这是单例设计的思想(可以参考HeadFirst设计模式)
*/
if(instance == null){
synchorinized(ImageLoader.class){
if(instance == null){
instance = new ImageLoader();
}
}
}
return instance;
}
public synchronized void init(){
if(configuration == null){
throw new IlleagalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);
if(this.configuration == null){
this.configuration = configuration;
emptyListener = new SimpleIageLoadingListener();
fakeBitmapDisplayer = new FakeBitmapDisplayer();
}
}
}
3.下载图片偏好
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub) // resource or drawable
.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
.showImageOnFail(R.drawable.ic_error) // resource or drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000)
.cacheInMemory(false) // default
.cacheOnDisk(false) // default
.preProcessor(...)
.postProcessor(...)
.extraForDownloader(...)
.considerExifParams(false) // default
.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.正式加载图片
两种方式:loadImage();displayImage()-一般用这个;
final ImageView mImageView = (ImageView) findViewById(R.id.image);
String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg";
ImageSize mImageSize = new ImageSize(100, 100); //显示图片的配置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build(); ImageLoader.getInstance().loadImage(imageUrl, mImageSize, options, new SimpleImageLoadingListener(){ @Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
mImageView.setImageBitmap(loadedImage);
} });
GirdView,ListView中使用
使用GridView,ListView来显示大量的图片,而当我们快速滑动GridView,ListView,我们希望能停止图片的加载,而在GridView,ListView停止滑动的时候加载当前界面的图片,这个框架当然也提供这个功能,使用起来也很简单,它提供了PauseOnScrollListener这个类来控制ListView,GridView滑动过程中停止去加载图片
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
参考
- https://github.com/nostra13/Android-Universal-Image-Loader
- http://blog.csdn.net/xiaanming/article/details/26810303
Universal-Image-Loader 使用步骤的更多相关文章
- 【Android应用开发】 Universal Image Loader ( 使用简介 | 示例代码解析 )
作者 : 韩曙亮 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50824912 相关地址介绍 : -- Universal I ...
- android universal image loader 缓冲原理详解
1. 功能介绍 1.1 Android Universal Image Loader Android Universal Image Loader 是一个强大的.可高度定制的图片缓存,本文简称为UIL ...
- universal image loader自己使用的一些感受
1.全局入口的Application定义初始化: ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Build ...
- 【译】UNIVERSAL IMAGE LOADER. PART 3---ImageLoader详解
在之前的文章,我们重点讲了Android-Universal-Image-Loader的三个主要组件,现在我们终于可以开始使用它了. Android-Universal-Image-Loader有四个 ...
- Android中Universal Image Loader开源框架的简单使用
UIL (Universal Image Loader)aims to provide a powerful, flexible and highly customizable instrument ...
- universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法
在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简介
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- 开源项目Universal Image Loader for Android 说明文档 (1) 简单介绍
When developing applications for Android, one often facesthe problem of displaying some graphical ...
- 【译】UNIVERSAL IMAGE LOADER.PART 2---ImageLoaderConfiguration详解
ImageLoader类中包含了所有操作.他是一个单例,为了获取它的一个单一实例,你需要调用getInstance()方法.在使用ImageLoader来显示图片之前,你需要初始化它的配置-Image ...
- 翻译:Universal Image Loader
本文转载于:http://blog.csdn.net/tianxiangshan/article/details/9399183 All manipulations are held by the I ...
随机推荐
- 一台机器上运行多个ActiveMq
由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.k ...
- POJ_3046_Ant_Counting_(动态规划,多重集组合数)
描述 http://poj.org/problem?id=3046 n种蚂蚁,第i种有ai个,不同种类的蚂蚁可以相互区分,但同一种类的蚂蚁不能相互区分,从这些蚂蚁中取出s,s+1,s+2,...,b- ...
- 比较详细的利用虚拟机对SD卡FAT32+EXT4+Ext4分区图解教程
如果大家嫌虚拟机复杂,我这里提供一个我没用虚拟机之前的分区方法:这个方法实际是可行的 我在没有用虚拟机之前,我是这样操作的1.首先在分区软件分好fat32+ext2+ext22.然后用recovery ...
- Oracle 12c最新特性
9 Pluggable Databases This section provides details on the Pluggable Databases (PDB) metrics. 9.1 Da ...
- JavaScript高级程序设计35.pdf
遍历 “DOM2级遍历和范围”模块定义了两个用于辅助完成顺序遍历DOM结构的类型:NodeIterator和TreeWalker,两个类型能够基于给定的起点对DOM结构执行深度优先(depth-fir ...
- C语言调用汇编实现字符串对换
1. 前面配置arm交叉编译环境. 2. 配置好qemu-arm C语言代码string-switch.c: #include <stdio.h> #include <stdlib. ...
- 普通用户从非80端口启动tomcat,通过端口转发监听80端口
linux下小于1024的端口都需要root去绑定. root权限启动tomcat是不明智的,可以使用非root权限启动tomcat监听8080端口,然后利用端口转发实现对80端口的监听. 端口转发: ...
- POJ 3125 Printer Queue
题目: Description The only printer in the computer science students' union is experiencing an extremel ...
- PHP学习笔记-00
PHP这门语言的就不用多说啦,使用率非常高的一门后端开发语言.之前一直希望可以学习了解一下PHP.之前主要在做Java和OC这类语言的开发,对于PHP这种脚本语言(动态语言)还是了解甚少. 近期看了一 ...
- C# - 类型
C#是一门使用OOP技术的编程语言(Object Oriented Programming 面向对象编程)面向对象最重要的特性就是接口.继承.多态 C#中所有的事物都可以看做是一个对象 对象由类型来创 ...