Android 如何在使用AsyncTask下载图像时显示默认图像?
我正在使用Android 如何在使用AsyncTask下载图像时显示默认图像?,android,listview,android-asynctask,Android,Listview,Android Asynctask,我正在使用AsyncTask从远程服务器下载图像,并在列表视图中显示它们。我正在使用来完成它,一切都很好 这个博客中唯一的问题是,在下载图像时,会显示一个彩色绘图。但我想在下载图像时显示默认图像。如下图所示: 在这里你可以看到: Taylor Roother和Oye Oye的照片已经下载 服务器上没有Gopala Govinda的图像,所以我没有 从服务器下载他的图像并显示默认图像 对于Hellboy,图像仍在下载中,因此需要一种颜色 背景(细黑线)显示在他的名字旁边I 我不想显示这条黑线。我
AsyncTask
从远程服务器下载图像,并在列表视图中显示它们。我正在使用来完成它,一切都很好
这个博客中唯一的问题是,在下载图像时,会显示一个彩色绘图。但我想在下载图像时显示默认图像。如下图所示:
在这里你可以看到:
Taylor Roother和Oye Oye的照片已经下载
服务器上没有Gopala Govinda的图像,所以我没有
从服务器下载他的图像并显示默认图像
对于Hellboy,图像仍在下载中,因此需要一种颜色
背景(细黑线)显示在他的名字旁边I
我不想显示这条黑线。我想显示默认值
图像就像在戈帕拉·戈文达面前一样,直到图像消失
从Hellboy的服务器加载
有人知道怎么做吗
或者,您可以在列表视图
的列表适配器
活动中从此处使用RemoteImageView,只需将默认图像放入图像视图
例如,在ListAdapter活动的getView中,您可以这样做:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.productrow, null);
}
TextView text=(TextView)vi.findViewById(R.id.textViewRow);;
ImageView imageView=(ImageView)vi.findViewById(R.id.productimage);
TextView price=(TextView)vi.findViewById(R.id.price);
ProductDetailModel model=productlist.get(position);
String result=model.getp_Set();
vi.setId(position);
text.setText(Html.fromHtml(result));
price.setText(" $ "+model.getp_R_Price());
// See here
imageView.setImageResource(R.drawable.stub_id);
return vi;
}
我使用以下开源库来管理异步任务中的图像获取:
它能够在下载实际图像时显示“正在加载”的图像
您可以使用此库或自己执行类似操作
如果您是自己做的,当您开始充气ImageView
时,请使用“加载”图像。然后在AsyncTask
中,图像下载后,您可以通过覆盖AsnycTask
的onPostExecute
方法来修改此视图(此方法在UI线程上运行,因此您可以在此方法中修改我们的视图)我认为您要做的是让下载的可绘制
类扩展位图可绘制
,而不是可绘制
,然后选择一个合适的超级构造函数来指定默认图像位图:
/**
* A fake Drawable that will be attached to the imageView while the download is in progress.
*
* Contains a reference to the actual download task, so that a download task can be stopped
* if a new binding is required, and makes sure that only the last started download process can
* bind its result, independently of the download finish order.</p>
*/
private static class DownloadedDrawable extends BitmapDrawable {
private final WeakReference<BitmapDownloaderTask>
bitmapDownloaderTaskReference;
private DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask,
Resources resources, Bitmap bitmap) {
super(resources, bitmap); // you'll have to provide these
bitmapDownloaderTaskReference =
new WeakReference<BitmapDownloaderTask>(bitmapDownloaderTask);
}
public BitmapDownloaderTask getBitmapDownloaderTask() {
return bitmapDownloaderTaskReference.get();
}
}
/**
*在下载过程中,将附加到imageView的一个假的Drawable。
*
*包含对实际下载任务的引用,以便可以停止下载任务
*如果需要新的绑定,请确保只有最后启动的下载进程可以
*绑定其结果,独立于下载完成顺序
*/
私有静态类DownloadedDrawable扩展了BitmapDrawable{
私人最终参考
bitmapDownloaderTaskReference;
私有下载可绘制(BitmapDownloaderTask BitmapDownloaderTask,
资源,位图(位图){
super(参考资料,位图);//您必须提供这些
位图下载程序task引用=
新WeakReference(位图下载任务);
}
公共BitmapDownloaderTask getBitmapDownloaderTask(){
返回bitmapDownloaderTaskReference.get();
}
}
伙计们,我已经在行布局xml文件中给出了一个imageview。实际上,ColorDrawable是由DownloadedDrawable类在问题中给出的链接中设置的。伙计们,我已经在行布局xml文件中给出了一个imageview。实际上,ColorDrawable是由问题中给出的链接中的DownloadedDrawable类设置的。