专栏首页向治洪开源项目Universal Image Loader for Android

开源项目Universal Image Loader for Android

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


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)

Everything is simple. We say, from which URL an image should be downloaded and in which ImageView it should be displayed. The view options (DisplayImageOptions) will be taken from configuration (defaultDisplayImageOptions (...)) in this case.


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

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; 


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


• whether to cache the loaded image in memory;


• whether to cache the downloaded image on file system.


• to decode the image as quickly as possible (DecodingType.FAST) or as economical for RAM as possible (DecodingType.MEMORY_SAVING).


So, we can pass these options every time by calling displayImage() method or we can specify default options in configuration for initialization; and they will be used in all cases when options weren’t explicitly passed by method calling.

 所以,我们可以在每次调用 displayImage() 方法的时候传入这些参数,或者调用默认的选项来初始化

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


 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:


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

2. You should configure the maxImageWidthForMemoryCache(...) and maxImageHeightForMemoryCache(...) parameters in configuration only if you want to load in the ImageView images with size larger than size of the device's screen (for example, for subsequent zooming). In all other cases, you don’t need this: these parameters consider the screen size by default for saving memory when working with Bitmaps.


3. Set thread pool size in the configuration wisely: a large pool size (> 10) will allow multiple threads to work simultaneously, which can significantly affect the UI work speed. But it can be fixed by setting a lower priority for threads: the lower priority is the more responsive UI is while ImageLoader work and the longer images are loaded. UI responsiveness is critical to the lists (smooth scrolling), so you should play around with setting of threadPoolSize(...) and threadPriority(...) parameters for selection of the optimal configuration for your application.

 在设置中配置线程池的大小是非常明智的。一个大的线程池会允许多条线程同时工作,但是也会显著的影响到UI线程的速度。但是可以通过设置一个较低的优先级来解决:当ImageLoader在使用的时候,可以降低它的优先级,这样UI线程会更加流畅。在使用List的时候,UI 线程经常会不太流畅,所以在你的程序中最好设置threadPoolSize(...)和threadPriority(...)这两个参数来优化你的应用。

4. memoryCacheSize(...) and memoryCache(...) settings overlap each other. Use only one of them for one configuration object.


5. discCacheSize(...), discCacheFileCount(...) and discCache(...) settings overlap each other, using only one of them for one configuration object.


6. If by using the ImageLoader in an application you always (or almost always) pass into the 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.


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

在图片解析的时候使用 FAST 或者 MEMORY_SAVING模式,并不会有明显的区别。但是如果要在list中显示的时候,建议使用FAST模式,Gallery中显示的时候建议使用MEMORY_SAVING模式。

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



0 条评论
登录 后参与评论


  • 高仿今日头条(2)

    接上一篇博客:http://blog.csdn.net/xiangzhihong8/article/details/51262939 这部分主要是实现频道管理页...

  • 仿qq最新侧滑菜单

    为了后续对这个项目进行优化,比如透明度动画、背景图的位移动画,以及性能上的优化。 我把这个项目上传到github上面,请大家随时关注。 github地址 htt...

  • Android Remote Views

    听名字就可以看出,remote views是一种远程view,感觉有点像远程service,其实remote views是view的一个结构,他可以在其他的进程...

  • ImageView的属性和方法大全


  • 算法训练 K好数

    如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数。求L位K进制数中K好数的数目。例如K = 4,L = 2的时候,所有...

  • Golang Leetcode 454. 4Sum II.go

    版权声明:原创勿转 https://blog.csdn.net/anakinsun/article/details/89143799

  • P1525 关押罪犯 并查集

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

  • hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/...

  • 15:整理药名

    15:整理药名 总时间限制: 1000ms 内存限制: 65536kB描述 医生在书写药品名的时候经常不注意大小写,格式比较混乱。现要求你写一个程序将医生书...

  • C++11新特性——range for

    很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中。range for语句,可以方便的遍历给定序列中的每个元素并对其...