How can universal image loader be used to set image to other views other than ImageView?

1.1k views Asked by At

I need to set an image to an ImageSwitcher by downloading it from server. But the universal image loader seems to take ImageView as a parameter.

I read that we can use ImageAware to display image on any android view.

Can someone help me how to go about this ?

Thanks, Sneha

3

There are 3 answers

1
Pedro Oliveira On BEST ANSWER

Use loadImage instead. That will get you a bitmap that you can use to do whatever you want with it.

// 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
    }
});
0
nostra13 On

You also can wrap your view with ImageAware interface. And then you can pass this wrapper into displayImage(...) method.

You can look into ImageViewAware to understand how it wraps ImageView. So you can wrap any view in the same way.

0
tesla1984 On

you can check that Loader display image for ImageAware finally

the code displayImage(String,ImageView) is below

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

And the ImageAware extends the abstract class ViewAware,here is the two important methods in it

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

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

check the two method implement by 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);
}

so you can see that the ImageView use setImageBitmap to display bitmap,while the other view will use setBackground,here is my class for other view

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));
}