Universal Image Loader. Part 3

Thursday, 15 January 2015 12:15

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.

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.

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.

The third option.

In addition, you can "listen" the process of image downloading and displaying using the interface 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.

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:
  2. ImageView imageView = new ImageView(getApplicationContext()); // Wrong!
    ImageView imageView = new ImageView(MyActivity.this); // Correctly
    ImageView imageView = new ImageView(getActivity()); // Correctly (for Fragments)
  3. 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.
  4. 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.
  5. memoryCacheSize(...) and memoryCache(...) settings overlap each other. Use only one of them for one configuration object.
  6. discCacheSize(...), discCacheFileCount(...) and discCache(...) settings overlap each other, using only one of them for one configuration object.
  7. 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.
  8. 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).
By IntexSoft - custom software development company


Item tags