我正在使用 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中view的回收允许把图片设置到错误的位置(直到图片还没有下载完)。
实际问题是这样的:
如果我打开我的 Activity (包含 GridView
),由于 UIL 库,显示的项目可以下载图像。 (同时 no_foto.png
图像显示在网格的每个 View 中)。当所有 View 都加载了自己的图像时,如果我尝试向下滚动然后返回(向上滚动),我必须等待图像重新加载,因为所有 View 现在都被 no_foto.png 占用
图片。
如何避免重新加载这些图像?使用 Universal Image Loader,我可以缓存之前加载的图像吗?
注意:因为我的网格可以包含很多图像,所以我会使用磁盘缓存
(不是内存缓存)的实现。
我发现.discCacheFileCount()
可以设置最大缓存文件数,为什么我的文件好像没有缓存?
最佳答案
我已经解决了这个问题
我只是声明了选项,但我不想使用它,所以我修改了这一行:
imageLoader.displayImage(basePath+immagine, iv);
进入:
imageLoader.displayImage(basePath+immagine, iv, options);
我在选项中添加了方法:
.cacheOnDisc(true)
关于android - 使用 Universal Image Loader 在 GridView 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764850/