【问题标题】:Android ImageDownloader: Adding indeterminate progress bar while downloading image in AsyncTaskAndroid ImageDownloader:在 AsyncTask 中下载图像时添加不确定的进度条
【发布时间】:2012-11-27 15:25:50
【问题描述】:

我正在使用 Gilles Debunne 在Multithreading For Performance 中描述的 ImageDownloader,我想做一个简单的扩展。

我想为每个尚未下载的列表项添加一个不确定的ProgressBar。我现在主要是通过调用来设置它:

progressBar.setVisibility(View.VISIBLE);    // in the AsyncTask preExecute()
progressBar.setVisibility(View.INVISIBLE);  // in the AsyncTask postExecute()

具体来说,只要在 postExecute() 中设置 imageView 的位图,我就会将进度条标记为不可见……就像这样:

if (this == bitmapDownloaderTask) {
    imageView.setImageBitmap(bitmap);
    progBar.setVisibility(View.INVISIBLE);
}

但我一直无法找到正确的测试来使preExecute() 中的进度条可见。我尝试了各种组合:

if (imageView == null)
if (imageView.getDrawingCache() == null)
if (downloadedDrawable == null)
if (downloadedDrawable.getColor() == Color.BLACK)

有人对如何使这项工作提出建议吗?那就是:对于这段代码,确定给定位图是否设置的正确测试是什么?谢谢!

【问题讨论】:

    标签: android caching android-asynctask android-progressbar


    【解决方案1】:

    为什么 imageView 在 preExecute 中会为空,这意味着你为什么需要测试?好的,在您引用的博客中,它使用了对图像视图的弱引用。你测试它是否为 null,就像他在 onPostExecute 中所做的那样:

            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
    

    发生这种情况的唯一方法是如果 AsyncTask 未启动但包含图像视图的 Activity 已完成并从屏幕上删除,这是可能的,因此最好对其进行测试。如果为null,则应立即调用cancel。

    preExecute 中不存在已下载的Drawable(例如位图),因为它尚未下载。

    你的测试:

    if (this == bitmapDownloaderTask) {
    

    似乎是多余的。什么时候不是bitmapDownloaderTask?

    [编辑] 但是为什么你需要测试 imageview 来显示进度条呢?为什么不在 onPreExecute 中显示(如果没有取消)并在 onPostExecute 中隐藏?

    【讨论】:

    • 感谢您的回复,但我没有完全听懂。您对我应该使用哪些条件在 onPreExecute() 中显示进度条,然后将其隐藏在 onPostExecute() 中是否有具体建议?
    猜你喜欢
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 2013-08-06
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多