Search code examples
androiduniversal-image-loader

Android Universal ImageLoader in ListView scrolling very slow


I have a ListView with items which have various numbers of images. Each item was put into a HorizontalScrollView with a horizontal LinearLayout, and I add ImageViews dynamically into the LinearLayout.

I used Android-Universal-Image-Loader to load images from local sdcard asynchronously. I set the ImageLoaderConfiguration in the onCreate method of custom Application class:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .threadPriority(Thread.NORM_PRIORITY - 2)
        .denyCacheImageMultipleSizesInMemory()
        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
        .diskCacheSize(200 * 1024 * 1024) // 200 Mb
        .tasksProcessingOrder(QueueProcessingType.LIFO)
        .writeDebugLogs() // Remove for release app
        .build();
ImageLoader.getInstance().init(config);

And set tha DisplayImageOptions in the constructor method of Adapter class:

options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.ic_loading)
        .showImageForEmptyUri(R.drawable.ic_loading_failed)
        .showImageOnFail(R.drawable.ic_loading_failed)
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .considerExifParams(true)
        .build();

And I load the images dynamically in the getView method:

ImageLoader.getInstance().displayImage(imageURI, imageView, options);

My problem is, when I scroll the ListView, the image will be reloaded very slow, actually about the same speed as the first load. And it seems like each image was loaded from disk again without caching.

PS: My images are mostly taken from camera, and have a size of about 2MB ~ 3MB. I'm not sure whether it's because the original image is too large.

Thanks for your advice!


Solution

  • Found what's happening.

    For short: cached image is too large.

    I used a relative size of ImageView: [wrap_content, match_parent] instead of setting fixed size. So the ImageLoader cache the max possible size of all the images as I saw in the log which is my device size: 1080x1800. And that is also very large and cause slow reloading.

    To solve it:

    Set fixed size of your ImageView. Or if you don't want to, set the max possible width and height of the ImageView. After I set the max height, my images were cached to 250x250 and the loading delay was almost gone.

    See Useful Info for more information.