0

我正在使用 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)

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

4

1 回答 1

0

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

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

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

下载的Drawable(例如位图)不会存在于preExecute 中,因为它还没有被下载。

您的测试:

if (this == bitmapDownloaderTask) {

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

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

于 2012-11-27T17:19:55.117 回答