2012-07-20 56 views
1

我使用https://github.com/thest1/LazyList進行圖像緩存。我必須全屏顯示圖像。但是圖像質量有很大的損失。 哪些代碼我不得不改變爲獲得原始image.thanks提前。Android:使用ImageLoader減少圖像質量

+0

你能告訴我你爲什麼要在全屏顯示圖像時使用LazyList。 – AB1209 2012-07-20 11:34:12

+0

@ AB1209爲什麼不在這種情況下使用LazyLoading。用戶可能不希望UI線程被掛起。這真的很好用.. – 2012-07-20 11:36:14

+0

@ AB1209我有很多圖片可以從網上下載並顯示到gallery-View中。 – Roshni 2012-07-20 11:44:08

回答

5

尋找在ImageLoader的類此方法,

private Bitmap decodeFile(File f){ 
    try { 
     //decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE=70; 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 
     int scale=1; 

      while(true){ 
       if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
        break; 
       width_tmp/=2; 
       height_tmp/=2; 
       scale*=2; 
      } 
     //decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 

,並刪除下面的線從這個方法,

while(true){ 
       if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
        break; 
       width_tmp/=2; 
       height_tmp/=2; 
       scale*=2; 
      } 

這將確保你的形象沒有得到根本縮放。

但是你必須記住,如果你這樣做,你的應用程序容易受到OOM的攻擊。

+1

是的,我同意Andro Selva,在某些情況下,您將通過刪除此代碼塊來解決內存異常問題 – Anand 2012-07-20 11:40:32

0

在你ImageLoader.java功能

//decodes image and scales it to reduce memory consumption 
    private Bitmap decodeFile(File f) 

用於縮放/調整UR圖像。

final int REQUIRED_SIZE=70; 

增加此項可提高圖像質量。使它200或東西,並嘗試。