【问题标题】:How can universal image loader be used to set image to other views other than ImageView?通用图像加载器如何用于将图像设置为 ImageView 以外的其他视图?
【发布时间】:2014-11-13 10:24:06
【问题描述】:

我需要通过从服务器下载图像来将图像设置为 ImageSwitcher。但是万能的图片加载器好像是以ImageView为参数的。

我读到我们可以使用 ImageAware 在任何 android 视图上显示图像。

有人可以帮我解决这个问题吗?

谢谢, 斯内哈

【问题讨论】:

  • Sneha,我对 Universal Image Loader 了解不多,但我认为 Android Query 可以使用它。

标签: android universal-image-loader imageswitcher


【解决方案1】:

请改用loadImage。这会给你一个位图,你可以用它来做任何你想做的事情。

// Load image, decode it to Bitmap and return Bitmap to callback
imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});

【讨论】:

  • 奥利维娅:感谢您的快速回复!它工作:)
【解决方案2】:

你也可以用ImageAware 接口包裹你的视图。然后你可以将这个包装器传递给displayImage(...) 方法。

您可以查看ImageViewAware 以了解它如何包装ImageView。所以你可以用同样的方式包装任何视图。

【讨论】:

    【解决方案3】:

    您最终可以检查 ImageAware 的 Loader 是否显示图像

    代码 displayImage(String,ImageView) 如下

    public void displayImage(String uri, ImageView imageView) {
        this.displayImage(uri, (ImageAware)(new ImageViewAware(imageView)), (DisplayImageOptions)null, (ImageLoadingListener)null, (ImageLoadingProgressListener)null);
    }
    

    ImageAware扩展了抽象类ViewAware,这里面有两个重要的方法

    protected abstract void setImageDrawableInto(Drawable var1, View var2);
    
    protected abstract void setImageBitmapInto(Bitmap var1, View var2);
    

    查看ImageAware实现的两种方法

    protected void setImageDrawableInto(Drawable drawable, View view) {
        ((ImageView)view).setImageDrawable(drawable);
        if(drawable instanceof AnimationDrawable) {
            ((AnimationDrawable)drawable).start();
        }
    
    }
    
    protected void setImageBitmapInto(Bitmap bitmap, View view) {
        ((ImageView)view).setImageBitmap(bitmap);
    }
    

    所以你可以看到 ImageView 使用 setImageBitmap 显示位图,而其他视图将使用 setBackground,这是我的类其他观点

    public class CustomViewAware extends ViewAware {
    public CustomViewAware(View view) {
        super(view);
    }
    
    @Override
    protected void setImageDrawableInto(Drawable drawable, View view) {
        view.setBackgroundDrawable(drawable);
    }
    
    @Override
    protected void setImageBitmapInto(Bitmap bitmap, View view) {
        view.setBackgroundDrawable(new BitmapDrawable(bitmap));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多