Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageDownloader:在异步任务中下载图像时添加不确定的进度条_Android_Caching_Android Asynctask_Android Progressbar - Fatal编程技术网

Android ImageDownloader:在异步任务中下载图像时添加不确定的进度条

Android ImageDownloader:在异步任务中下载图像时添加不确定的进度条,android,caching,android-asynctask,android-progressbar,Android,Caching,Android Asynctask,Android Progressbar,我正在使用Gilles Debunne在中描述的ImageDownloader,我想做一个简单的扩展 我想在每个尚未下载的列表项中添加一个不确定的ProgressBar。我现在主要通过拨打以下电话进行设置: progressBar.setVisibility(View.VISIBLE); // in the AsyncTask preExecute() progressBar.setVisibility(View.INVISIBLE); // in the AsyncTask postE

我正在使用Gilles Debunne在中描述的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)

有人对如何实现这一目标有什么建议吗?也就是说:此代码的正确测试是什么,以确定给定位图是否已设置?谢谢

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

        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
唯一可能发生这种情况的方法是,如果AsyncTask未启动,但包含图像视图的活动已完成并从屏幕上删除,这是可能的,因此最好对其进行测试。如果为空,则应立即调用cancel

preExecute中不存在下载的可绘制文件(如位图),因为它尚未下载

您的测试:

if (this == bitmapDownloaderTask) {
这似乎是多余的。什么时候不是bitmapDownloaderTask


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

谢谢您的回复,但我没有完全理解。对于我应该使用哪些条件在onPreExecute()中显示进度条,然后在onPostExecute()中隐藏进度条,您有什么具体建议吗?