Scrolling through ListView with some images very laggy

ⅰ亾dé卋堺 提交于 2019-12-05 08:12:45

The problem is that each image is decoded at the moment the view is prepared for showing. The ListView will recycle your views that means that at the moment a view leaves the screen it will be reused and therefore the image will be overwritten and garbage collected. If the item reenters the screen the image has to be decoded from the database again.

The decoding is reasonable fast but if the user changes the position in the list very quickly all the decoding calls will make your list very laggy.

I would implment something like an ImageCache. A class that holds a Map with WeakReferences to images. Everytime you want to display an image you take a look if the image is allready in the map and if the WeakReference still points to an object, if this is not the case you need to decode the image and then store it in the map.

Take a look at the lazy loading questions, these will show you how to put the decoding in a background task and then update the list the moment the images are loaded. It is a bit more effort but it can make the list much much more faster. If you are going to use the code example from the lazy loading questions try to use AsyncTasks instead of Threads to do the decoding in.

Here is sth, what you should read, and it should help you to solve problem (whole Displaying Bitmaps Efficiently part): http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

If your images are not the proper size, they are being scaled on the fly, which is not a cheap operation, particularly for large images. Be sure that you are loading thumbnails, not the wallpaper, out of your database.

Also, you can use Traceview to find out where your time is being spent.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!