【问题标题】:How to show default image while image is being downloaded using AsyncTask?使用 AsyncTask 下载图像时如何显示默认图像?
【发布时间】:2012-09-12 10:43:40
【问题描述】:

我正在使用AsyncTask 从远程服务器下载图像并在ListView 中显示它们。我正在使用 this blog 来完成它,并且一切正常。

此博客中唯一的问题是在下载图像时会显示 ColorDrawable。但是我想在下载图像时显示默认图像。如下图所示:

在这里你可以看到:

  1. Taylor Loother 和 Oye Oye 的图片已下载。
  2. 服务器上没有 Gopala Govinda 的图像,所以我没有 从服务器为他下载图片并显示默认图片。
  3. 对于地狱男爵来说,图片还在下载中,所以一个颜色 背景(细黑线)出现在他的名字旁边。 我 不想显示这条黑线。我想显示默认值 图像 就像在 Gopala Govinda 前面一样,直到图像不是 为地狱男爵从服务器加载。

    有人知道怎么做吗?

【问题讨论】:

    标签: android listview android-asynctask


    【解决方案1】:

    我认为您想要做的是让DownloadedDrawable 类扩展BitmapDrawable 而不是Drawable,然后选择适当的超级构造函数之一来指定您的默认图像位图:

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

    【讨论】:

      【解决方案2】:

      我使用以下开源库来管理 AsyncTasks 中的图像获取: UrlImageViewHelper on GitHub

      它能够在下载实际图像时显示“正在加载”图像。

      您可以使用这个库或自己做类似的事情。

      如果你是自己做的,当你最初给你的ImageView 充气时,使用“加载”图像。然后在你的AsyncTask中,一旦你的图片被下载,你可以通过覆盖AsnycTaskonPostExecute方法来修改这个视图(这个方法在UI线程上运行,所以你可以在这个方法中修改我们的视图)

      【讨论】:

        【解决方案3】:

        或者,您可以从这里https://github.com/kaeppler/ignition/wiki/Sample-applications 使用 RemoteImageView

        【讨论】:

        • 伙计们,我已经在行布局xml文件中给出了一个imageview。实际上 ColorDrawable 是由问题中给出的链接中的 DownloadedDrawable 类设置的。
        【解决方案4】:

        在您的ListViewListAdapter Activity 中,只需将默认图像放入ImageView

        例如,在 ListAdapter Activity 的 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;
        }
        

        【讨论】:

        • 伙计们,我已经在行布局xml文件中给出了一个imageview。实际上 ColorDrawable 是由问题中给出的链接中的 DownloadedDrawable 类设置的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-06
        • 2019-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-02
        相关资源
        最近更新 更多