I am wondering if which of the two is better in loading images in a listview from web, is it by batch through some number of threads that are running simultaneously or one by one through thread queue?
I have noticed (but I don’t know if that is really the implementation) from the youtube app that the images are loaded by batch and it is kinda fast. Even for not only loading images but also requesting some data from the web as well. Does anyone have an idea?
“better” in which way? Performance wise? Developer-friendliness? Usability wise?
A couple fundamental things to consider:
- Creating threads is expensive. It’s slow and each thread consumes system resources (of course). When creating a single thread for each download, use a managed capped thread pool.
- Don’t load images if they’re not visible to the user. What you should do is in
ListAdapter, check whether the image has already been loaded, and if not, re-use a thread from the thread pool to do the work.
- Be careful with AsyncTask. As far as I know, AsyncTask manages a fixed, application wide thread pool (I think it’s capped to 5 threads), so if all these threads are busy loading images, any other task you perform through that class will block.
- Don’t re-invent the wheel. Does the ImageLoader of Droid-Fu solve your problem? It also implements a cache, so images aren’t downloaded twice.
The best way to have this kind of functionality is to have the images ‘lazy load’ in the list. If your list is of fixed size then run multiple threads (one for each visible list item), download the images and refresh the images in the list. In that mean time have some dummy image placed in the same location.
Have only a fixed number of image components for you list, preferably a few more then the total visible images at any point. Each time the list is scrolled, check whether the image corresponding to the that particular list item exists or not. If yes, display. If not, display the dummy image, run the thread to load the image in background and refresh the list image once download completes.
To further save the memory you can use ‘SoftReferences’ for the image components. This allows the garbage collector to take away the images that are not being shown on the screen at the moment.
I tried to create a simple lazy list example that may be used as a reference, here it is Lazy load of images in ListView
this might be what you are looking for, uses a fixsized threadpool and a soft cache to lazyload images.