简体   繁体   中英

Dynamically changing Image in ImageView

I have an viewPager activity with fragment. It starts asyncTask to download information about six thumbnails and on postExecute creates six objects of my image class with information and bitmap object null. When object is created it downloads image. Constructor:

    public MyImage(String name, String owner, String time, String date,
        String total_rate, String votes, String description) {
    //this.thumbnail = BitmapFactory.
    Log.d(TAG, "Created image object");
    this.name = name;
    this.owner = owner;
    this.time = time;
    this.date = date;
    this.total_rate = Double.valueOf(total_rate);
    this.votes = votes;
    this.description = description;

    downloadThumbnail();
}

The fragment has a gridView in it with six thumbnails. I can set default image to it or progressDialog, but how do I change it after I have downloaded the image? Set up a listener? PS I don't know a lot about adapters, problem might be there.

you can add a callback as follows: first implement a class:AsyncImageLoader public class AsyncImageLoader {

 public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
         final Handler handler = new Handler() {
             public void handleMessage(Message message) {
                 imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
             }
         };
         new Thread() {
             @Override
             public void run() {
                 //TODO SOMETHING
                 ......
             }
         }.start();
         return null;
     }

public interface ImageCallback {
         public void imageLoaded(Drawable imageDrawable, String imageUrl);

     }

Then you can do like this: AsyncImageLoader asyncImageLoader = new AsyncImageLoader(); ...... Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl,new ImageCallback() { public void imageLoaded(Drawable imageDrawable, String imageUrl) {

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM