【问题标题】:slow performance of android listview due to add contact image in my listview由于在我的列表视图中添加联系人图像,android 列表视图的性能变慢
【发布时间】:2018-03-13 20:28:12
【问题描述】:

我在演示中列出了联系人 Uri 中的所有联系人,因为我在添加联系人图像后创建了自定义列表视图,我的列表视图滚动非常慢,以下是我的代码。

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.loadContactPhoto(activity, uri, R.drawable.icon,
                    null);
            return bitmap;
        }
        return null;
    }

在方法的帮助下,我获得了我在 getView 方法中使用的联系人照片。喜欢

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

它工作正常,但列表视图性能低。

请帮助我提高列表视图的性能。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    我建议将图像加载移至后台任务。 从另一个线程,将所有图像加载到内存(或者您可以根据可见内容仅对列表的一部分进行一些缓存),一旦图像准备好,更新 UI。 功能行为是,起初,一些图像不会立即显示,但性能会好得多。

    看看here

    【讨论】:

    • 嗨,感谢您的回复,我尝试使用此功能,但在该课程中,我从 Internet 下载图像,我是从联系人 uri 获取的。
    • 无论你是从互联网下载、从光盘加载还是其他任何东西,加载图像都需要 I/O,你只需要避免保持 UI 线程(例如 getView)同时你会得到联系人图片。
    【解决方案2】:

    也许缓存数据会提高性能?尝试将此添加到您的代码中:

    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;
    }    
    

    这至少可以防止每次调用 getView() 时都创建位图。

    根据这个特定类的生命周期,您可能会也可能不会将缓存设为静态和/或将其状态保存到文件以供以后加载。

    【讨论】:

      【解决方案3】:

      您需要异步加载图像。

      这是一个讨论这个问题的线程。

      Lazy load of images in ListView

      this response 有源代码作为源链接提及

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

      【讨论】:

      • 复制 Lior Ohana 的答案。
      • 回复有答案的链接。
      • @cjk:啊,现在我正确地阅读了您的评论。我在发我的时候没有注意到。
      猜你喜欢
      • 2015-08-28
      • 2012-02-08
      • 1970-01-01
      • 2023-03-22
      • 2014-06-09
      • 1970-01-01
      • 2019-06-03
      • 2016-09-24
      相关资源
      最近更新 更多