Optimization of ListView in Android
Today, I learned some optimization schemes about ListView under my buddy's leadership. I would like to share with you now ....
First:
ViewHolder is often used for data loading in Listview. We need to write it as static class, so that data is shared in the local cache, And the retrieved data is very fast.
Second point:
If the Listview needs to load images, we usually use a sub-thread to load data and then update the data in the main thread. In this way, we often set a default image for a good user experience. If the data is still being loaded, we will first display the default image and then slowly load the image.
holder.img.setImageDrawable(defDrawable);holder.img.setBackgroundResource(R.raw.back);
The above two methods both set the default image, but there is a big gap in efficiency.
The workflow of holder. img. setImageDrawable (defDrawable) is to add a Drawable, and then draw this picture with the Android painting mechanism. To do this, we need to first obtain this image in the listView adapter constructor.
Resources res = mContext.getResources();Bitmap bitmap = BitmapFactory.decodeResource(res, R.raw.back);defDrawable = new BitmapDrawable(res, bitmap);
We first find this image under the raw file, load it into the memory, then convert it into a bitmap object, and then convert it into a drawable object, in this way, the original painting mechanism of Android can quickly draw this image. In addition, we can only generate one object from start to end during image loading without wasting memory.
Holder. img. setBackgroundResource (R. raw. back) the workflow is to first load the images in the raw folder to the memory, then convert them into bitmap objects, and then convert them into drawable objects. Each default image generates an object. If we have thousands or even tens of thousands of images, this may cause memory overflow and the program will catch it.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tdrI/bXjo7q/tLz7yc/examples + samples + 87Sv + samples/Na7u + G809TYztLK1ta41 + signature + 3dDFz6KjrMeww + signature + zL2bustq + Signature SKOsu + G40L71tb3T0L + rotate/qtS0v/rotate + G63LrDtcQuLi48L3A + rotate "brush: java;" >/// first load the first page image if (! TextUtils. isEmpty (item. picUrl) {if (position <4) {loadIMG (holder. img, item );}}