【译】UNIVERSAL IMAGE LOADER.PART 2---ImageLoaderConfiguration详解

简介:

ImageLoader类中包含了所有操作。他是一个单例,为了获取它的一个单一实例,你需要调用getInstance()方法。在使用ImageLoader来显示图片之前,你需要初始化它的配置-ImageLoaderConfiguration使用init(…)方法。然后,你就可以使用可以明确地根据需要使用不同形式的displayImage(…)。

总之,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")
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration .createDefault(context));
imageLoader.displayImage(imageUrl, imageView);

现在,让我们看看完整的用法。

就像你已经知道的,我们首先需要用configuration对象来初始化ImageLoader。因为ImageLoader是一个单例,所以它需要在App一启动的时候就初始化。我建议在一个重载了了的Application.onCreate()来做这件事。对于一个已经初始化的ImageLoader,重新初始化将不会对程序有任何影响。

接下来,我们创建一个configuration,他是一个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();
复制代码

下面让我们来看看每一个选项。

• maxImageWidthForMemoryCache() 和maxImageHeightForMemoryCache()用于将图片将图片解析成Bitmap对象。为了不储存整个图片,根据ImageView参数的值(要加载图片的那个)减少图片的大小。maxWidth和maxHeight(第一阶段),layout_width layout_height(第二阶段)。如果不定义这些参数(值为fill_parent和wrap_content被视为不确定的大小),然后尺寸的设定就会根据maxImageWidthForMemoryCache()和maxImageHeightForMemoryCache()的设置而定。原始图像的大小最大会缩小到2倍(适合用fast decoding),直到宽度或高度变得小于指定值;

o默认值 - 设备的屏幕大小

• httpReadTimeout()设置图片从网络中加载的最大超时时间(以毫秒为单位)

o 默认值- 30秒

• threadPoolSize() 设置线程池的大小. 每一个图片加载和显示的任务都是在一个个单独的线程中进行的,这些线程在从图片网络中被下载的时候就会进入线程池。因此,池的大小决定能同时运行的线程数。一个大的线程池能显著地拖慢UI的响应速度,例如,列表的滚动就会变慢. 
o 默认值- 5

• threadPriority()设置正在运行任务的所有线程在系统中的优先级(1到10);

o默认值- 4

• 调用denyCacheImageMultipleSizesInMemory()强制UIL在内存中不能存储内容相同但大小不同的图像。由于完整大小的图片会存储在磁盘缓存中,后面当图片加载进入内存,他们就会缩小到ImageView的大小(图片要显示的尺寸),然而在某些情况下,相同的图像第一次显示在一个小的View中,然后又需要在一个大的View中显示。同时,两个不同大小的相同内容的图片就会被将被存储在内存中。这是默认的操作。denyCacheImageMultipleSizesInMemory()指令确保删除前一个加载的图像缓存的内存的大小

• 使用memoryCache(),你可以指定内存缓存的实现。你可以使用现成的解决方案(他们都是实现limited  size-cache,如果超过缓存大小,就通过一定算法删除一个对象):

o FIFOLimitedCache (根据先进先出的原则上删除多余对象)
o LargestLimitedCache (空间占用最大的对象会被删除)
o UsingAgeLimitedCache (最早被添加的对象会被删除)
o UsingFreqLimitedCache (最少被用到的对象会被删除)
或者,你可以实现通过实现接口MemoryCacheAware <String,Bitmap>来实现自己的缓存;
o 默认值 - 使用2 MB的内存限制的UsingFreqLimitedCache
memoryCacheSize() 设置内存缓存的最大占用空间. 在这种情况下,默认的缓存策略是 - UsingFreqLimitedCache.
o 默认值 - 2 MB

• Using discCache(), 你可以定义自己的磁盘缓存. 也可以现成的解决方案 (文件跟特定的URL匹配,文件名为这些URL的哈希值):

o UnlimitedDiscCache (通常的策略, 缓存大小没有限制)
o FileCountLimitedDiscCache (限定大小的缓存)
o TotalSizeLimitedDiscCache (限定文件个数的缓存策略)
另外,你也可以通过实现DiscCacheAware接口定义自己的缓存
o 默认值 - UnlimitedDiscCache

• 使用defaultDisplayImageOptions(),你可以设置image显示选项,如果自定义选项没有传递给displayImage(),它将用于displayimage(…)方法的每一次调用。下面,我将详细讨论这些选项。

我们可以构建一个configuration对象或信任一个开发人员(比如我)然后使用默认的configuration:

 

ImageLoaderConfiguration config =
ImageLoaderConfiguration.createDefault(context);

 

因此,configuration就创建好了。现在,ImageLoader可以通过它初始化了:

ImageLoader.getInstance().init(config);

就这样,ImageLoader已经可以使用了。在下一篇文章中我们还会讲到它。

 

 

原文链接

http://www.intexsoft.com/blog/item/72-universal-image-loader-part-2.html


作者:kissazi2 
出处:http://www.cnblogs.com/kissazi2/ 
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

转载:http://www.cnblogs.com/kissazi2/p/3903899.html

目录
相关文章
|
9月前
UE Load texture images at runtime Plugin description
UE Load texture images at runtime Plugin description
56 0
|
Java
SAP Spartacus Sample Data Extension
SAP Spartacus Sample Data Extension
97 0
SAP Spartacus Sample Data Extension
SAP Spartacus cost-center.config.ts里定义的paramsMapping
SAP Spartacus cost-center.config.ts里定义的paramsMapping
SAP Spartacus cost-center.config.ts里定义的paramsMapping
A simple tool to calculate the total size of a BSP application
Today Ben asks me whether there is some tool which can allow us to get a draft estimation on the size of a BSP application. As far as I know there is no such tool, so I write one by myself:
99 0
A simple tool to calculate the total size of a BSP application
GM6 pageset - Cache get scenario /ui2/cl_pfcg_utils
Created by Wang, Jerry, last modified on Apr 20, 2015
124 0
GM6 pageset - Cache get scenario /ui2/cl_pfcg_utils
PREDEFINED_TYPES sap.ui.base.DataType.getType image
PREDEFINED_TYPES sap.ui.base.DataType.getType image
101 0
PREDEFINED_TYPES sap.ui.base.DataType.getType image
|
JSON jenkins 持续交付
成功解决mxnet.base.MXNetError: C:\Jenkins\workspace\mxnet-tag\mxnet\3rdparty\dmlc-core\src\io\local_file
成功解决mxnet.base.MXNetError: C:\Jenkins\workspace\mxnet-tag\mxnet\3rdparty\dmlc-core\src\io\local_file
成功解决mxnet.base.MXNetError: C:\Jenkins\workspace\mxnet-tag\mxnet\3rdparty\dmlc-core\src\io\local_file