【问题标题】:Loading images in GridView using Universal Image Loader使用通用图像加载器在 GridView 中加载图像
【发布时间】:2013-09-16 20:59:03
【问题描述】:

我正在使用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中视图的回收导致将图片设置在错误的位置(直到图片没有被下载)。

实际的问题是这样的:

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

如何避免重新加载这些图像?使用 Universal Image Loader,我可以缓存之前加载的图像吗?

注意:由于我的网格可以包含许多图像,我会使用disk cache 的实现(不是内存缓存)。 我发现.discCacheFileCount()可以用来设置缓存文件的最大数量,为什么我的文件好像没有缓存呢?

【问题讨论】:

  • UIL 工作正常,您是否尝试在下载后在图像视图上使用 postinvalidate() ? (请注意,我在 3 个活动/列表中具有相同的问题)
  • @Joseph82 你能告诉我你如何解决我也面临同样的问题吗?但我正在显示来自 sdcard 的图像
  • @user3233280 在下面看我的答案:stackoverflow.com/a/18765198/1584654
  • @Joseph82 你能告诉我我将如何写选项吗???我已经声明了选项,但我没有在选项中写任何东西
  • DisplayImageOptions options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.no_foto) .showImageForEmptyUri(R.drawable.no_foto) .showImageOnFail(R.drawable.no_foto) .build(); 使用你想要的drawable,而不是no_foto

标签: android android-imageview android-view android-gridview universal-image-loader


【解决方案1】:

我已经解决了问题

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

imageLoader.displayImage(basePath+immagine, iv);

进入:

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

我在选项中添加了方法:

.cacheOnDisc(true)

【讨论】:

    【解决方案2】:

    UIL默认不启用缓存,所以如果你想使用缓存,你应该使用

    // Create default options which will be used for every 
    //  displayImage(...) call if no options will be passed to this method
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
        ...
        .cacheInMemory()
        .cacheOnDisc()
        ...
        .build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
        ...
        .defaultDisplayImageOptions(defaultOptions)
        ...
        .build();
    ImageLoader.getInstance().init(config); // Do it on Application start
    

    在加载图片时使用:

    // Then later, when you want to display image
    ImageLoader.getInstance().displayImage(imageUrl, imageView); // Default options will be used
    

    另一种方法是

    DisplayImageOptions options = new DisplayImageOptions.Builder()
        ...
        .cacheInMemory()
        .cacheOnDisc()
        ...
        .build();
    ImageLoader.getInstance().displayImage(imageUrl, imageView, options); 
    

    您可以找到更多信息here

    【讨论】:

    • 我想为gridview中的所有项目设置不同的图像存根,我该怎么做?
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 2016-06-23
    相关资源
    最近更新 更多