2013-05-14 96 views
1

在我的ListView中,每行都有一個ImageView和兩個TextView。 ListView的圖片從我的Galaxy Nexus的內存中加載。我已經將它縮減到100x100使用ListView中的位圖:緩慢加載

Bitmap scaled = Bitmap.createScaledBitmap(de, 80, 80, true); 

但它仍然需要幾秒鐘加載。

我該怎麼辦?

編輯:

public Bitmap resizeBitmap(String path){ 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    InputStream is = null; 
    try { 
     is = new FileInputStream(path); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    BitmapFactory.decodeStream(is,null,options); 
    try { 
     is.close(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    try { 
     is = new FileInputStream(path); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    // here w and h are the desired width and height 
    options.inSampleSize = Math.max(options.outWidth/100, options.outHeight/100); 
    // bitmap is the resized bitmap 
    Bitmap bitmap = BitmapFactory.decodeStream(is,null,options); 
    return bitmap; 
} 
+0

使用lazyloading – mukesh 2013-05-14 11:32:21

回答

0

顯示ListView中的位圖有效地是一個相當複雜的工作,涉及到相當多的變數。我建議你閱讀this並下載他們提供的代碼示例。這是一個很好的起點,在簡單的情況下可能會被複制粘貼。

+0

我已經試過它與我編輯我的問題的代碼。 – user896692 2013-05-14 11:04:32

1

第一點我可以從你的代碼中看到,你正在解碼流兩次......解碼它的第一個地方,它甚至不在任何地方使用....因此,刪除該語句將提高你的執行速度碼。

//BitmapFactory.decodeStream(is,null,options); comment this line 
try { 
    is.close(); 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 

也可以問你爲什麼不能使用圖片的縮略圖?而不是調整每個圖像....如果你想在你的應用程序來顯示圖像的縮略圖

是的,有一個完整的表存儲的圖像,你需要通過遊標API來訪問它例如thumbanil:

Cursor mediaCursor = managedQuery(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, 
      null, null); 

從這個遊標就可以得到圖像標識,然後用圖片ID的幫助下,你可以通過使用例如下面的MediaStore.Images.Thumbnails.getThumbnail()方法retreive圖像的縮略圖是一些代碼,這將有助於:

 Bitmap thumbBitmap = null; 

    thumbBitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, imageID, MediaStore.Images.Thumbnails.MICRO_KIND, null); 
    if(thumbBitmap != null){ 
     return new BitmapDrawable(thumbBitmap); 
    } 
+0

有沒有辦法使用縮略圖?搜索了很多,但只發現了降低原始位圖的可能性 – user896692 2013-05-14 11:18:49

+0

是的,看看我的編輯答案 – 2013-05-14 11:26:53

+0

是「cr」在第二個代碼示例mediacursor? – user896692 2013-05-14 12:10:06