我有一个带有片段的viewPager活动。它启动asyncTask以下载有关六个缩略图的信息,并且在postExecute上创建我的图像类的六个对象,其中信息和位图对象为null。创建对象时,它会下载图像。构造:
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();
}
片段中有一个带有六个缩略图的gridView。我可以设置默认图像或progressDialog,但是如何在下载图像后更改它?设置一个监听器?附:我不太了解适配器,可能存在问题。
答案 0 :(得分:0)
您可以按如下方式添加回调: 首先实现一个类:AsyncImageLoader 公共类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);
}
然后你可以这样做: AsyncImageLoader asyncImageLoader = new AsyncImageLoader(); ...... Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl,new ImageCallback(){ public void imageLoaded(Drawable imageDrawable,String imageUrl){
}
});