In the previous article, we’ve initialized the ImageLoader with configuration; and now, it is ready for immediate use according to its intended purpose.

 在之前的章节,我们已经通过配置初始化了ImageLoader,现在,我们来介绍一下怎么使用ImageLoader

For this, it has four overloaded methods:

有下面四个重载的方法



void displayImage(String url, ImageView view)
void displayImage(String url, ImageView view, DisplayImageOptions options)
void displayImage(String url, ImageView view, ImageLoadingListener listener)
void displayImage(String url, ImageView view, DisplayImageOptions options, ImageLoadingListener listener)



 

The first option.

void displayImage(String url, ImageView view)

DisplayImageOptions) will be taken from configuration (defaultDisplayImageOptions (...)) in this case.

最简单的方式,我们只需要定义要显示的图片的URL和要显示图片的ImageView。这种情况下,图片的显示选项会使用默认的配置

 

The second option.

void displayImage(String url, ImageView view, DisplayImageOptions options)

We already can define certain options for a specific task. First, I’ll give an example of creating my own options: 

我们可以自定义一个显示选项。下面是一个例子


DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUrl(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.decodingType(DecodingType.MEMORY_SAVING)
.build();



 

Yes, Builder again. As mentioned in the first article, we can specify using DisplayImageOptions:

如同我们在第一篇文章中提到的,我们可以自定义图片的显示选项:

• whether to display the stub image in ImageView, while the real image is downloading, and what image should be displayed; 

加载过程中是否显示图片的stub,如果显示的话显示那个

• whether to display the stub image in ImageView if empty image URL was passed, and what image should be displayed;

URL错误的时候,是否显示一个stub,如果显示的话显示那一个

• whether to cache the loaded image in memory;

是否在内存中缓存已加载图片

• whether to cache the downloaded image on file system.

是否缓存已下载图片到本地

DecodingType.FAST) or as economical for RAM as possible (DecodingType.MEMORY_SAVING).

快速解析图片或者经济模式

displayImage()

 displayImage() 方法的时候传入这些参数,或者调用默认的选项来初始化

In addition, you can "listen" the process of image downloading and displaying using the interface ImageLoadingListener:The third option.

ImageLoadingListener监听图片下载和现实的过程



public interface ImageLoadingListener {
void onLoadingStarted();
void onLoadingFailed();
void onLoadingComplete();
}

 

And the fourth option is the most powerful one. You can both define options and "listen" to the process.

void displayImage(String url, ImageView view, DisplayImageOptions options, ImageLoadingListener listener)

这是最强大的方法,你既可以定制选项有可以监听过程。

Tips and tricks

小提示:

1. To perform its functions, the ImageLoader should receive correct parameters. And the point is ImageView rather than image URL. If you create an ImageView object in code (not using LayoutInflater), then pass the current Activity to constructor, and not the application context:

要想实现ImageLoader的功能,你必须传递进去正确的参数。关键点是ImageView要比URL重要。如果你在代码中创建了一个ImageView对象,那么在构造函数中你就要把当前的Activity传递进去作为Context,而不是Application作为Context

ImageView imageView = new ImageView(getApplicationContext()); //错误

ImageView imageView = new ImageView(MyActivity.this); //正确
ImageView imageView = new ImageView(getActivity()); // 正确 (用于 Fragments)


 

maxImageWidthForMemoryCache(...) and maxImageHeightForMemoryCache(...)

maxImageWidthForMemoryCache(...)和maxImageHeightForMemoryCache(...)这两个参数,比如放大图片的时候。其他情况下,不需要做这些配置,因为默认的配置会根据屏幕尺寸以最节约内存的方式处理Bitmap。

threadPoolSize(...) and threadPriority(...)

threadPoolSize(...)和threadPriority(...)这两个参数来优化你的应用。

memoryCacheSize(...) and memoryCache(...)

 这两个参数会互相覆盖,所以在Configuration中使用一个就好了

discCacheSize(...), discCacheFileCount(...) and discCache(...)

 这三个参数会互相覆盖,只使用一个

displayImage(...)method the same loading options (DisplayImageOptions), then a reasonable solution would be setting these options in the ImageLoader configuration as default options (defaultDisplayImageOptions(...) method). Then, you should not indicate these options by calling displayImage(...). If options aren’t explicitly given to the method, then default option will be used for this task.

displayImage()方法时传入的参数经常是一样的,那么一个合理的解决方法是,把这些选项配置在ImageLoader的设置中作为默认的选项(通过调用defaultDisplayImageOptions(...)方法)。之后调用displayImage(...)方法的时候就不必再指定这些选项了,如果这些选项没有明确的指定给defaultDisplayImageOptions(...)方法,那调用的时候将会调用UIL的默认设置。

7. There is no significant difference between FAST and MEMORY_SAVING decoding types, but it is recommended to use FAST for all kinds of lists (where you want to display many images of small size), and MEMORY_SAVING for galleries (where you want to display images of large size).

MEMORY_SAVING模式。

 

So, I've completed my story about the Universal Image Loader. The project sources are available on ​​GitHub​​.


In the previous article, we’ve initialized the ImageLoader with configuration; and now, it is ready for immediate use according to its intended purpose.

 在之前的章节,我们已经通过配置初始化了ImageLoader,现在,我们来介绍一下怎么使用ImageLoader

For this, it has four overloaded methods:

有下面四个重载的方法



void displayImage(String url, ImageView view)
void displayImage(String url, ImageView view, DisplayImageOptions options)
void displayImage(String url, ImageView view, ImageLoadingListener listener)
void displayImage(String url, ImageView view, DisplayImageOptions options, ImageLoadingListener listener)



 

The first option.

void displayImage(String url, ImageView view)

DisplayImageOptions) will be taken from configuration (defaultDisplayImageOptions (...)) in this case.

最简单的方式,我们只需要定义要显示的图片的URL和要显示图片的ImageView。这种情况下,图片的显示选项会使用默认的配置

 

The second option.

void displayImage(String url, ImageView view, DisplayImageOptions options)

We already can define certain options for a specific task. First, I’ll give an example of creating my own options: 

我们可以自定义一个显示选项。下面是一个例子



DisplayImageOptions options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_image)
.showImageForEmptyUrl(R.drawable.image_for_empty_url)
.cacheInMemory()
.cacheOnDisc()
.decodingType(DecodingType.MEMORY_SAVING)
.build();


 

Yes, Builder again. As mentioned in the first article, we can specify using DisplayImageOptions:

如同我们在第一篇文章中提到的,我们可以自定义图片的显示选项:

• whether to display the stub image in ImageView, while the real image is downloading, and what image should be displayed; 

加载过程中是否显示图片的stub,如果显示的话显示那个

• whether to display the stub image in ImageView if empty image URL was passed, and what image should be displayed;

URL错误的时候,是否显示一个stub,如果显示的话显示那一个

• whether to cache the loaded image in memory;

是否在内存中缓存已加载图片

• whether to cache the downloaded image on file system.

是否缓存已下载图片到本地

DecodingType.FAST) or as economical for RAM as possible (DecodingType.MEMORY_SAVING).

快速解析图片或者经济模式

displayImage()

 displayImage() 方法的时候传入这些参数,或者调用默认的选项来初始化

In addition, you can "listen" the process of image downloading and displaying using the interface ImageLoadingListener:The third option.

ImageLoadingListener监听图片下载和现实的过程



public interface ImageLoadingListener {
void onLoadingStarted();
void onLoadingFailed();
void onLoadingComplete();
}

 

And the fourth option is the most powerful one. You can both define options and "listen" to the process.

void displayImage(String url, ImageView view, DisplayImageOptions options, ImageLoadingListener listener)

这是最强大的方法,你既可以定制选项有可以监听过程。

Tips and tricks

小提示:

1. To perform its functions, the ImageLoader should receive correct parameters. And the point is ImageView rather than image URL. If you create an ImageView object in code (not using LayoutInflater), then pass the current Activity to constructor, and not the application context:

要想实现ImageLoader的功能,你必须传递进去正确的参数。关键点是ImageView要比URL重要。如果你在代码中创建了一个ImageView对象,那么在构造函数中你就要把当前的Activity传递进去作为Context,而不是Application作为Context



ImageView imageView = new ImageView(getApplicationContext()); //错误

ImageView imageView = new ImageView(MyActivity.this); //正确
ImageView imageView = new ImageView(getActivity()); // 正确 (用于 Fragments)


 

maxImageWidthForMemoryCache(...) and maxImageHeightForMemoryCache(...)

maxImageWidthForMemoryCache(...)和maxImageHeightForMemoryCache(...)这两个参数,比如放大图片的时候。其他情况下,不需要做这些配置,因为默认的配置会根据屏幕尺寸以最节约内存的方式处理Bitmap。

threadPoolSize(...) and threadPriority(...)

threadPoolSize(...)和threadPriority(...)这两个参数来优化你的应用。

memoryCacheSize(...) and memoryCache(...)

 这两个参数会互相覆盖,所以在Configuration中使用一个就好了

discCacheSize(...), discCacheFileCount(...) and discCache(...)

 这三个参数会互相覆盖,只使用一个

displayImage(...)method the same loading options (DisplayImageOptions), then a reasonable solution would be setting these options in the ImageLoader configuration as default options (defaultDisplayImageOptions(...) method). Then, you should not indicate these options by calling displayImage(...). If options aren’t explicitly given to the method, then default option will be used for this task.

displayImage()方法时传入的参数经常是一样的,那么一个合理的解决方法是,把这些选项配置在ImageLoader的设置中作为默认的选项(通过调用defaultDisplayImageOptions(...)方法)。之后调用displayImage(...)方法的时候就不必再指定这些选项了,如果这些选项没有明确的指定给defaultDisplayImageOptions(...)方法,那调用的时候将会调用UIL的默认设置。

7. There is no significant difference between FAST and MEMORY_SAVING decoding types, but it is recommended to use FAST for all kinds of lists (where you want to display many images of small size), and MEMORY_SAVING for galleries (where you want to display images of large size).

MEMORY_SAVING模式。

 

So, I've completed my story about the Universal Image Loader. The project sources are available on ​​GitHub​​.