2011-09-02 40 views
1

我必須做出演示,列出從接觸烏里所有的接觸中,我已經接觸式圖像的加入我的列表視圖Scrolling的是檸慢,如下因素是後發的自定義列表視圖中添加聯繫人圖片我的代碼。緩慢的性能,因爲在我的lisview

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

它的工作正常,但列表視圖性能低。

請幫我改進listview的性能。

感謝 Milind

回答

2

我建議把這個圖像加載到後臺任務。 從另一個線程,將所有圖像加載到內存(或者您可以根據可見內容對列表的一部分進行緩存),一旦圖像準備就緒,請更新UI。 功能行爲將是,首先,一些圖像將不會立即顯示,但性能會更好。

看看here

+0

您好,感謝性反應的我嘗試這一點,但在從互聯網上該類下載圖片在我的情況下,我從聯繫uri。 – milind

+0

如果從互聯網上下載,從光盤或其他任何地方加載,加載圖像需要I/O,並且您只需要避免在獲取聯繫人圖像時持有UI線程(例如getView)即可。 –

+0

我認爲這是最好的選擇,我嘗試... – milind

1

或許緩存中的數據會提高性能?嘗試添加這對您的代碼:

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()被調用。

根據這個特殊階層的生活,你可能會或可能不會進行緩存的靜態和/或保存其狀態到文件供以後加載。