开发者

Which is better loading of images for ListView?

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 d开发者_如何学Pythonata 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:

  1. 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.
  2. Don't load images if they're not visible to the user. What you should do is in getView() of your ListAdapter, check whether the image has already been loaded, and if not, re-use a thread from the thread pool to do the work.
  3. 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.
  4. 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


https://picard.hgo.se/~jakeri03/blog/?p=39

this might be what you are looking for, uses a fixsized threadpool and a soft cache to lazyload images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜