android – 使用通用图像加载程序在GridView中加载图像

前端之家收集整理的这篇文章主要介绍了android – 使用通用图像加载程序在GridView中加载图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Universal Image Loader 1.8.6库来加载从网络拍摄的图像.

ImageLoaderConfiguration配置如下:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
    .memoryCacheExtraOptions(480,800) // default = device screen dimensions
    .threadPoolSize(3) // default
    .threadPriority(Thread.NORM_PRIORITY - 1) // default
    .denyCacheImageMultipleSizesInMemory()
    .memoryCacheSize(2 * 1024 * 1024)
    .memoryCacheSizePercentage(13) // default
    .discCacheSize(50 * 1024 * 1024)
    .discCacheFileCount(100)
    .imageDownloader(new BaseImageDownloader(getApplicationContext())) // default
    .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
    .writeDebugLogs()
    .build();

DisplayImageOptions配置为:

DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showStubImage(R.drawable.no_foto)
    .showImageForEmptyUri(R.drawable.no_foto)
    .showImageOnFail(R.drawable.no_foto)
    .build();

我的ArrayAdapter里面的方法getView包含代码

iv.setImageResource(R.drawable.no_foto); 
imageLoader.displayImage(basePath+immagine,iv);

上述代码的第一行只是为了避免在gridView中的视图的回收允许将图像设置在错误的位置(直到图片尚未下载).

实际的问题是这样的:

如果我打开我的Activity(包含GridView),所显示的项目感谢UIL库可以下载图像. (同时,网格的每个视图中都显示no_foto.png图像).当所有视图都加载了自己的图像时,如果我尝试向下滚动,然后我回来(向上滚动),我必须等待图像被重新加载,因为所有的视图现在被no_foto.png图像占据.

如何避免重新加载这些图像?使用通用图像加载程序,可以缓存先前加载的图像吗?

注意:由于我的网格可以包含许多图像,我将使用磁盘缓存(而不是内存缓存)的实现.
我发现.discCacheFileCount()可以用于设置缓存文件的最大数量,为什么我的文件似乎没有被缓存?

解决方法

我已经解决了这个问题

我只是声明选项,但我不会使用它,所以我修改了这一行:

imageLoader.displayImage(basePath+immagine,iv);

成:

imageLoader.displayImage(basePath+immagine,iv,options);

我在选项中添加了以下方法

.cacheOnDisc(true)
原文链接:https://www.f2er.com/android/310813.html

猜你在找的Android相关文章