通用图像加载器

时间:2013-03-19 12:30:02

标签: android universal-image-loader

我正在使用nostra的通用图片加载器https://github.com/nostra13/Android-Universal-Image-Loader

我遇到了displayImage()的问题。发生以下情况:

“我调用了imageView.displayImage()并且没有加载图像(它在线程中运行) 然后我第二次调用imageView.displayImage(),因为第二个图像较小,第二个图像显示在imageView中,但几秒后第一个图像替换第二个图像。“

如何避免第二张图像被上一张图片替换?有什么选择可以设置吗?

3 个答案:

答案 0 :(得分:3)

在imageLoader.loadImage()的onLoadingComplete()回调中,我们得到imageUri作为参数,用当前的imageUrl检查它是否相同,然后在imageView中设置图像。请注意,当前imageUrl应该是成员变量而不是最终变量

答案 1 :(得分:1)

我希望以下代码能为您提供帮助

private void LoadBackground(){
    ParseFile f = messageObject.getParseFile("image");
    voiceFile = messageObject.getParseFile("");
    if( f == null) return;
    String fpath = f.getUrl().toString();
    if(f.getUrl() == null) return;
    if (!fpath.equalsIgnoreCase("")) {
        if (!fpath.startsWith("http://") && !fpath.startsWith("https://")) {
            Log.d("refreshProfile",fpath);
            File imgFile = new File(fpath);
            Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            if (bitmap != null) {
                imgBg.setImageBitmap(bitmap);
            }
        } else {
            ImageLoader.getInstance().displayImage(fpath, imgBg,
                DisplayImageOption.getDisplayImage(),
                new AnimateFirstDisplayListener(){
                    @Override
                    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                        pbLoading.setVisibility(View.GONE);
                    }
                });
        }
    }
}

以下是监听器的代码

public class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

  public static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

     @Override
     public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
       if (loadedImage != null) {
           ImageView imageView = (ImageView) view;
           boolean firstDisplay = !displayedImages.contains(imageUri);
           if (firstDisplay) {
               FadeInBitmapDisplayer.animate(imageView, 500);
               displayedImages.add(imageUri);
           }
       }
   }
}

DisplayImageOption.java

public class DisplayImageOption {

   public static DisplayImageOptions getDisplayImage() {

       DisplayImageOptions options = new DisplayImageOptions.Builder()
       .showImageOnLoading(R.drawable.background)
       .showImageForEmptyUri(R.drawable.background)
       .showImageOnFail(R.drawable.background)
       .cacheInMemory(true)
       .cacheOnDisk(true)
       .considerExifParams(true)
       .build();
       return options;

   }

   public static DisplayImageOptions getDisplayRoundedImage() {

       DisplayImageOptions options = new DisplayImageOptions.Builder()
       .showImageOnLoading(R.drawable.ic_launcher)
       .showImageForEmptyUri(R.drawable.ic_launcher)
       .showImageOnFail(R.drawable.ic_launcher)
       .cacheInMemory(true)
       .cacheOnDisk(true)
       .considerExifParams(true)
       .displayer(new RoundedBitmapDisplayer(100))
       .build();

       return options;

   }
}

答案 2 :(得分:0)

也许你可以这样做:

new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.default_avatar_female)
        .showImageOnFail(R.drawable.default_avatar_female)
        .showImageOnLoading(R.drawable.default_avatar_female).build();

by showImageOnLoading。