一、说明文档以及相关细节java
All manipulations are held by the ImageLoader class. It is a singletone, so to get a single instance of the class, you should call the getInstance() method. Before using ImageLoader to its intended purpose (to display images), you should initialize its configuration - ImageLoaderConfiguration using the init (...) method. Well, then you can use all variations of the displayImage(...) method with a clear conscience.android
全部的操做都由ImageLoader控制。该类使用单例设计模式,因此若是要获取该类的实例,须要调用getInstance()方法。在使用ImageLoader显示图片以前,你首先要初始化它的配置,调用ImageLoaderConfiguration的init()方法,而后你就能够实现各类的显示了。web
In general, the easiest option of using ImageLoader (with the default configuration) is shown below:设计模式
一般状况下,ImageLoader最简单的使用方法以下(使用默认的配置选项):缓存
ImageView imageView = ... // view, where the image will be displayed网络
String imageUrl = ... // image URL (e.g. "http://site.com/image.png", "file:///mnt/sdcard/img/image.jpg")app
ImageLoader imageLoader = ImageLoader.getInstance();less
imageLoader.init(ImageLoaderConfiguration.createDefault(context));ide
imageLoader.displayImage(imageUrl, imageView);ui
Now, let’s consider the full functionality.
如今让咱们讨论下他的所有功能:
As you already know, you first need to initialize the ImageLoader using the configuration object. As the ImageLoader is a singleton, then it should be initialized only once for application launching. I would recommend doing it in an overloaded Application.onCreate(). A reinitializing of an already initialized ImageLoader will have no effect.
So, we create a configuration, it is an object of the ImageLoaderConfiguration class. We create it using theBuilder:
就像你已经知道的,首先,你须要使用ImageLoaderConfiguration对象来初始化ImageLoader。因为ImageLoader是单例,因此在程序开始的时候只须要初始化一次就行了。建议你在Activity的onCreate()方法中初始化。若是一个ImageLoader已经初始化过,再次初始化不会有任何效果。下面咱们经过ImageLoaderConfiguration.Builder建立一个设置
File cacheDir = StorageUtils.getCacheDirectory(context,
"UniversalImageLoader/Cache");
ImageLoaderConfiguration config = new
ImageLoaderConfiguration.Builder(getApplicationContext())
.maxImageWidthForMemoryCache(800)
.maxImageHeightForMemoryCache(480)
.httpConnectTimeout(5000)
.httpReadTimeout(20000)
.threadPoolSize(5)
.threadPriority(Thread.MIN_PRIORITY + 3)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedCache(2000000)) // You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.build();
Let’s consider each option:
下面上咱们来讨论一下每一个选项:
• maxImageWidthForMemoryCache() and maxImageHeightForMemoryCache() is used for decoding images into Bitmap objects. In order not to store a full-sized image in the memory, it is reduced to a size determined from the values of ImageView parameters, where the image is loaded: maxWidth and maxHeight (first stage),layout_width and layout_height (second stage). If these parameters are not defined (values fill_parentand wrap_content are considered as uncertain), then dimensions specified by settings maxImageWidthForMemoryCache() and maxImageHeightForMemoryCache() are taken. The size of the original image is reduced by 2 times (recommended for fast decoding), till the width or height becomes less than the specified values;
•用于将图片解码为Bitmap对象。为了不将原图存到内存中,系统会根据ImageView的参数来缩小图片的尺寸,这些参数包括maxWidth 、maxHeight 、layout_width 、layout_height .若是这些参数没有指定,尺寸将会根据maxImageWidthForMemoryCache和maxImageHeightForMemoryCache指定。原始图片的尺寸将会被缩减两次,知道宽和高比指定的值小。
o Default values - size of the device’s screen.
o 默认值 - 设备屏幕的尺寸
• httpConnectTimeout() sets the maximum waiting time (in milliseconds) for establishing an HTTP connection;
• 设置创建HTTP链接的最大超时时间
o Default value - 5 seconds
o 默认值 - 5秒
• httpReadTimeout() sets the maximum time (in milliseconds) for loading an image from the Web;
• 设置从网络上加载图片的最大超时时间
o Default value - 30 seconds
o 默认值 - 30秒
• threadPoolSize() sets size of the thread pool. Each task on image loading and displaying is performed in a separate thread, and those threads, in which the image uploading from the Web occurs, get to the pool. Thus, the pool size determines the number of threads running simultaneously. Setting of a large pool size can significantly reduce the speed of the UI, for example, list scrolling could slow down.
• 设置线程池的大小。每个加载和显示图片的任务都运行在独立的线程中,所以,线程池的大小决定了能够同时运行的线程数,若是设置的过大,将会下降UI线程的反应速度,好比List滑动时可能会卡顿。
o Default value - 5
o 默认值 - 5
• threadPriority() sets priority of all threads in the system (from 1 to 10), in which tasks are performed;
•设置当前线程的优先级(1 -- 10)
o Default value - 4
o 默认值 - 4
• calling denyCacheImageMultipleSizesInMemory() imposes a ban on storing different sizes of the same image in the memory. As full-size images are stored in the disk cache, and when loading into memory, they are reduced to the size of ImageView, in which they should be displayed, then there are cases when the same image has to be displayed first in a small view, and then in a big one. At the same time, two Bitmaps of different sizes representing the same image will be stored in the memory. This is the default behavior.
• 调用该方法会禁止在内存中缓存同一张图片的多个尺寸。当把本地图片加载到内存中时,首先会把图片缩减至要显示的ImageView的大小,所以可能会出现一种情况,就是会首先显示一张图的小图,而后再显示这张图的大图。这种状况下,同一张图片的两种尺寸的Bitmap会被存储在内存中,这是默认的操做
The denyCacheImageMultipleSizesInMemory() instruction ensures deletion of the previous size of the loaded image from cache in the memory.
该方法会确保删除已加载图片缓存在内存中的其余尺寸的缓存。
• Using memoryCache(), you can specify the implementation of cache in the memory. You can use ready-made solutions (they all are realizations of limited size-cache; where by exceeding cache size, an object is removed from it by a certain algorithm):
• 调用该方法,你能够指定在内存中缓存的实现。你可使用以下这些已有的解决方案
o FIFOLimitedCache (the object is removed on the basis of First-In-First-Out)
o 按照FIFO规则清理内存
o LargestLimitedCache (the largest-sized object is removed)
o 移除最大的缓存
o UsingAgeLimitedCache (the object with the oldest date of access is removed)
o 按照访问时间,移除最久以前的缓存
o UsingFreqLimitedCache (the most rarely used object is removed)
o 按照使用频率,移除最少使用的缓存
Alternatively, you can implement your own version of the cache by implementing the interface MemoryCacheAware<String, Bitmap>;
固然,你也经过实现接口 MemoryCacheAware<String, Bitmap>来实现一个自定义的清除缓存的方法,
o Default value - UsingFreqLimitedCache with memory limit to 2 MB
o 默认值 - UsingFreqLimitedCache ,缓存大小2MB
memoryCacheSize() sets the maximum cache size in the memory. In this case, the default cache is used -UsingFreqLimitedCache.
设置内存中缓存的大小。这种状况下,默认的缓存方法是用UsingFreqLimitedCache
o Default value - 2 MB
o 默认值 - 2MB
• Using discCache(), you can define cash implementation in the file system. You can use ready-made solutions (where the files matching certain URLs are named as hash codes of these URLs):
• discCache()设置本地缓存。你可使用如下已实现的方法
o UnlimitedDiscCache (usual cache, no restrictions)
o 不限制缓存大小
o FileCountLimitedDiscCache (cache with limited size)
o 限制缓存的文件数量(楼主可能把注释写反了)
o TotalSizeLimitedDiscCache (cache with limited files number)
o 限制缓存文件的总大小
Alternatively, you can define your own cache implementation by DiscCacheAware interface.
固然你能够实现接口DiscCacheAware 来自定义缓存的实现方法
o Default value - UnlimitedDiscCache
o 默认值 - UnlimitedDiscCache
• discCacheSize(int) specifies the maximum cache size in the file system. In this case, the TotalSizeLimitedDiscCache is used.
• 指定在本地的最大缓存大小。在这种状况下,TotalSizeLimitedDiscCache 会被使用
• discCacheFileCount(int) specifies the maximum number of files in the disk cache. In this case, the FileCountLimitedDiscCache is used.
• 指定在本地缓存的文件数量。在这种状况下,FileCountLimitedDiscCache 会被使用
• Using defaultDisplayImageOptions(), you can set image displaying options, which will be used for all calls of the displayimage(...) method, where custom options were not passed.
•使用defaultDisplayImageOptions你能够设置图片的显示选项,这些选项会在调用displayimage(...)的时候被使用。
I’ll discuss these options in details below.
在下面我会讨论这些选项
We can construct a configuration object ourselves or trust a developer (i.e. me) and use the default configuration:
咱们能够自定义配置选项,也能够trust me,使用我提供的默认选项:
ImageLoaderConfiguration config =
ImageLoaderConfiguration.createDefault(context);
Thus, the configuration is created. Now, the ImageLoader can be initialized with it:
这样,一个设置就建立了,以后,你就能够用它来初始化ImageLoader
ImageLoader.getInstance().init(config);
That's all, the ImageLoader is ready to use. I'll tell you about this in the next article.
这时候ImageLoader就可使用了。
二、使用范例
1.自定义XXXAppllication类,初始化ImageLoader
public class XXXApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
//这下面的参数就不一一解释了
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.discCacheSize(50 * 1024 * 1024)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.discCacheFileCount(100)
.writeDebugLogs()
.build();
ImageLoader.getInstance().init(config);
}
}
2.在AndroidManifest.xml文件的application标签里加入
[java]
android:name=".XXXApplication"
3.使用imageloader
[java]
public class ImageManager{
public static void Load(String imgUrl,ImageView imageView){
ImageLoader.getInstance().displayImage(imgUrl, imageView);
}
public static void Load(String imgUrl,ImageView imageView,DisplayImageOptions o){
ImageLoader.getInstance().displayImage(imgUrl, imageView,o);
}
}
固然对于图片的显示上是能够不一样的状况采用不一样的配置来显示,若是用默认构造方法的话,那么显示的效果就是你在
XXXAppllication类中定义好的效果了,若是,有一部分是须要特殊处理的,好比说要有圆角,那么就须要本身单独的再写一下配置,在当前的类的构造方法或者onCreate();中初始化,以下
private DisplayImageOptions options;
public MyPhotoEditAdapter(Context context,List<String> imgUrlList){
this.context=context;
this.imgUrlList=imgUrlList;
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.icon_default2x)
.showImageForEmptyUri(R.drawable.icon_default2x)
.cacheOnDisc() // 缓存用
.displayer(new RoundedBitmapDisplayer(8)) // 图片圆角显示,值为整数
.build();
}
在使用的时候一行代码就能够直接显示了
ImageLoader.getInstance().displayImage(图片的网络地址Url, 显示图片的控件ImageView,这个就是你本身写的配置了options);
使用默认配置的方式显示图片就更简单了
ImageLoader.getInstance().displayImage(图片的网络地址uri,显示图片的控件 imageView);
最后千万要记得
在AndroidManifest.xml文件的application标签里加入
android:name=".XXXApplication"
不然你的程序就会报错,提示ImageLoader没有实例化
OK,简单的解析就到这里,欢迎你们一块儿讨论