开发者

slow performance of android listview due to add contact image in my listview

开发者 https://www.devze.com 2023-04-01 18:29 出处:网络
I have make on demo that list all contact from contact Uri in that I have made custom list view after adding of contact image my listview scrolling is very slow, following is my code.

I have make on demo that list all contact from contact Uri in that I have made custom list view after adding of contact image my listview scrolling is very slow, following is my code.

public Bitmap getProfilepicture(Activity activity, String address)
    {
            Bitmap bitmap;
            Uri personUri = Uri
                .withAppendedPath(Phones.CONTENT_FILTER_URL, address);
            Cursor phoneCursor = activity.getContentResolver().query(personUri,
                PHONE_PROJECTION, null, null, null);
        if (phoneCursor.moveToFirst()) {

            int indexPersonId = phoneCursor.getColumnIndex(Phones.PERSON_ID);
            long personId = phoneCursor.getLong(indexPersonId);

            phoneCursor.close();

            Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
            bitmap = People.load开发者_开发知识库ContactPhoto(activity, uri, R.drawable.icon,
                    null);
            return bitmap;
        }
        return null;
    }

with the help of method I getting contact photo that i use in my getView method. like

holder.imgViewLogo.setImageBitmap(getProfilepicture(activity,pos));

it's working proper but listview performance is low.

please help me out improve performance of listview.


I suggest moving the image loading to a background task. From another thread, load all images to memory (or you can do some caching for only part of the list according to what is visible), once an image is ready, update the UI. The functional behavior will be that at first, some images will not be shown immediately but performance will be much better.

Take a look here


Perhaps caching the data would improve performance? Try adding this to your code:

private static HashMap<String address, Bitmap image> cache = new HashMap<String address, Bitmap image>();

public Bitmap getProfilepicture(Activity activity, String address) {
    if (cache.containskey(address) return cache.get(address);

    Bitmap bitmap;
       .
       .
       .
    bitmap = People.loadContactPhoto(activity, uri, R.drawable.icon,
                null);
    cache.put(address,bitmap);
    return bitmap;
}    

This would at least prevent the bitmap from being created every time getView() is called.

Depending on the life of this particular class, you may or may not make the cache static and/or save its state to file for later loading.


you need to load the images asyncronesly.

here is a thread which discusses about this.

Lazy load of images in ListView

and this response has source code mentioned as source links

GitHub: https://github.com/thest1/LazyList
Source: http://open-pim.com/tmp/LazyList.zip

0

精彩评论

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

关注公众号