2014-09-19 180 views
2

我有一个列表视图,我想显示其中的视频缩略图,但每次我滚动图像再次下载。我怎样才能下载一次图像并显示它。 这是我的代码:使用通用图像加载器在列表视图中加载图像

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    if(convertView == null){ 
     rootView = mInflater.inflate(R.layout.adapter_video, parent, false); 
     holder = new ViewHolder(); 
     holder.ivThumbnail = (ImageView) rootView.findViewById(R.id.ivThumbnailVideoAdapter); 
     rootView.setTag(holder); 
    } 
    else{ 
     rootView = convertView; 
     holder = (ViewHolder)rootView.getTag(); 
    } 
    imageLoader.displayImage(update.get(position).getThumbnail(), holder.ivThumbnail, option); 
    return rootView; 
} 
+0

我该如何启用它,以及如何使用它,因为我启用缓存 – 2014-09-19 14:36:49

+0

那么如何,你知道它被再次下载? ...服务器可能是一个问题fx它不会对'If-Modified-Since'或其他缓存相关头文件做出反应 – Selvin 2014-09-19 14:37:45

+0

imageview不会显示任何东西,直到再次下载图像 – 2014-09-19 14:38:49

回答

1

与UIL图像的下载配置了DisplayImageOptions

例如:

mOptionsSimple = new DisplayImageOptions.Builder().resetViewBeforeLoading(true) 
                   .cacheOnDisc(true) 
                   .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 
                   .bitmapConfig(Bitmap.Config.RGB_565) 
                   .build(); 

这里,cacheOnDisc为true在保存图像磁盘,不在缓存中。当你滚动图片时会重新加载,当然是因为在listview中单元格被回收。但有了这个选项,它将重新加载磁盘,如果它已经下载

你也可以/或启用memoryCache但要小心这个。它可以是OutOfMemoryException异常的原因,如果图像不管理correclty

ImageLoaderConfiguration这里有一个例子:

config = new ImageLoaderConfiguration.Builder(getApplicationContext()).threadPoolSize(3) 
                  .threadPriority(Thread.NORM_PRIORITY - 1) 
                  .tasksProcessingOrder(QueueProcessingType.FIFO) 
                  .imageDownloader(new MyImageDownloader(mContext)) 
                  .imageDecoder(new BaseImageDecoder(false)) 
                  .discCache(new UnlimitedDiscCache(cacheDir)) 
                  .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
                  .defaultDisplayImageOptions(mOptionsSimple) 
                  .writeDebugLogs() 
                  .build(); 

在最后这个例子中,discCache不限,如果你认为这可以改变它会占用太多的磁盘空间

+0

感谢您的代码,它的工作,但我惊讶,因为我在内存中使用缓存,但它再次下载。也之前我使用缓存磁盘上,但它再次下载,但我认为你使用bitmapConfig,它的工作,我错了吗? – 2014-09-19 14:52:28

+0

当你说“它再次下载”你看到日志中的痕迹?还是图像只是觉得它正在加载? – 2014-09-19 15:00:29

+0

谢谢,@ An-droid你的代码已经解决了这个问题。 – 2017-09-10 17:46:48

相关问题