通用图像加载器 - 在多个视图中使用相同的图像

时间:2015-04-26 03:33:10

标签: android imageview universal-image-loader

我使用Universal Image Loader从后端加载图像以在列表中显示用户图像;但是,如果图标多次显示,则通用图像加载器不会填写所有视图。

  • [用户图1] - 无图像
  • [用户图1] - 无图像
  • [用户图片2] - 很好
  • [用户图像2] - 无图像
  • [用户图3] - 很好
  • [用户图1] - 很好

然后在另一个屏幕上:

  • [用户图1] - 很好
  • [用户图1] - 无图像

我正在使用cacheInMemory和cacheOnDisk,这似乎可以改善它。和以前一样,它只是在其中一个视图中显示,而不是大多数,但我需要所有这些视图才能工作。

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheInMemory( true )
            .cacheOnDisk( true )
            .build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( this )
            .threadPoolSize( 3 )
            .defaultDisplayImageOptions( defaultOptions )
            .build();

ImageLoader.getInstance().init( config );

我没有使用ListView执行此任务,我使用ScrollView并使用自定义布局对其进行充气。

private View createSmallActivity( LayoutInflater inflater, final Event activity ) {

    final View view;
    view = inflater.inflate( R.layout.activity_posted_small, null );
    ...
    // The owner's image.
    if( activity.ownerImageUrl != null ) {
        Loader.loadImage( getActivity(),
               activity.ownerImageUrl,
               R.drawable.postedactivitysmall_imageprofileempty,
               ( ImageView ) view.findViewById( R.id.profileImage ) );
    }
    return view;
}



// Loader.loadImage
// Setting the targetSize, and masking the image with a resource.
public static void loadImage( Context context, String url, int resource, ImageView view ) {
    Drawable d = context.getResources().getDrawable( resource );
    int h = d.getIntrinsicHeight();
    int w = d.getIntrinsicWidth();

    ImageSize targetSize = new ImageSize( w, h );
    ImageLoader.getInstance().loadImage( url, targetSize, new MaskImageLoader( context, view, resource ) );
}

如何改进Universal Image Loader以确保所有视图都已正确填写?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

这是因为Universal Image Loader使用相同的url(用作id)取消先前的请求。要防止此行为,请替换

ImageLoader.getInstance().loadImage( url, targetSize, new MaskImageLoader( context, view, resource ) );

通过

ImageLoader.getInstance().displayImage(url, 
    new NonViewAware(new ImageSize(w, h), ViewScaleType.CROP),
    new MaskImageLoader(context, view, resource));

答案 1 :(得分:0)

我建议你使用ListView。也许你应该全局声明ImageLoader。然后使用它。

ImageLoader imageLoader = ImageLoader.getInstance();
...
imageLoader.displayImage(imageUri, imageView);