2012-07-20 125 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或东西,并尝试。