开源项目Universal Image Loader for Android 说明文档 (2)

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 overloadedApplication.onCreate().A reinitializing of an already initialized ImageLoader will have no effect.

So, we create a configuration, it is an object of theImageLoaderConfigurationclass. 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()andmaxImageHeightForMemoryCache()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:maxWidthandmaxHeight(first stage),layout_widthandlayout_height(second stage). If these parameters are not defined (valuesfill_parentandwrap_contentare considered as uncertain), then dimensions specified by settingsmaxImageWidthForMemoryCache()andmaxImageHeightForMemoryCache()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;

、、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)

oDefault value – 4

o 默认值 – 4

callingdenyCacheImageMultipleSizesInMemory()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.

总结失败的原因能够让人越来越谨慎。

开源项目Universal Image Loader for Android 说明文档 (2)

相关文章:

你感兴趣的文章:

标签云: