如何显示默认的形象,而形象正在使用的AsyncTask下载? [英] How to show default image while image is being downloaded using AsyncTask?

查看:149
本文介绍了如何显示默认的形象,而形象正在使用的AsyncTask下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的AsyncTask 从远程服务器下载图片,并显示在一个的ListView 。我使用的 这个博客 来完成它,一切工作正常。

在这个博客唯一的问题是,ColorDrawable正在显示正在下载的形象,同时。但我想说明一个默认的图像被下载的形象,同时。就像在下面所附的图片:

在这里,您可以看到:

  1. 泰勒Loother和奥耶·奥耶图片已经被下载。
  2. 没有为帕拉文达在服务器上没有图像,所以我不 下载形象深入人心,从服务器并显示默认图像。
  3. 有关的Hellboy,图像仍然被下载的,所以彩色 背景(细黑线)被显示出来反对他的名字。 我 不想显示这条黑线。我想显示默认 图片就像在帕拉文达的前部,直至图像不 从服务器的Hellboy装载

    有没有人有任何想法如何做到这一点?

解决方案

我想你想要做的是让 DownloadedDrawable 类扩展 BitmapDrawable 而不是绘制对象然后再选择适当的超级构造函数之一来指定默认的图像的位图:

  / **
 *一个假绘制对象将被连接到ImageView的同时,下载过程中。
 *
 *包含一个参考实际下载任务,以便下载任务可以停止
 *如果一个新的绑定是必需的,并确保只有最后开始下载过程可以
 *绑定独立下载最终命令的结果,与LT; / P>
 * /
私有静态类DownloadedDrawable扩展BitmapDrawable {
    私人最终的WeakReference< BitmapDownloaderTask>
       bitmapDownloaderTaskReference;

    私人DownloadedDrawable(BitmapDownloaderTask bitmapDownloaderTask,
       资源资源,位图位图){
        超(资源,位图); //你必须提供这些
        bitmapDownloaderTaskReference =
            新的WeakReference< BitmapDownloaderTask>(bitmapDownloaderTask);
    }

    公共BitmapDownloaderTask getBitmapDownloaderTask(){
        返回bitmapDownloaderTaskReference.get();
    }
}
 

I am using AsyncTask to download images from remote server and show them in a ListView. I am using this blog to accomplish it and everything is working fine.

The only problem in this blog is that a ColorDrawable is being shown while the image is being downloaded. But I want to show a default image while the image is being downloaded. Just like in the attached image below:

Here you can see that:

  1. Images for Taylor Loother and Oye Oye have already been downloaded.
  2. There is no image for Gopala Govinda on the server , so I am not downloading image for him from the server and showing Default image.
  3. For Hellboy, the image is still being downloaded , so a color background (the thin black line) is showing up against his name. I don't want to show this black line . I want to show the default image just like in front of Gopala Govinda until the image is not loaded from the server for Hellboy.

    Does anyone have any idea how to do that?

解决方案

I think what you want to do is to have the DownloadedDrawable class extend BitmapDrawable instead of Drawable and then choose one of the appropriate super constructors to specify your default image bitmap:

/**
 * 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();
    }
}

这篇关于如何显示默认的形象,而形象正在使用的AsyncTask下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆