0

这是我的BitmapWorkerTask将图像加载到ViewPager。我正在尝试为每个页面添加一个水平ProgressBar以获得更好的用户体验,以显示图像加载的进度。 (我已经使用了'不确定'的圆形进度条。)Android:如何将Progressbar添加到BitmapWorkerTask?

BitmapWorkerTask是否甚至是添加进度条的正确位置,如果是的话该怎么做?

/** 
* The actual AsyncTask that will asynchronously process the image. 
*/ 
private class BitmapWorkerTask extends AsyncTask<Object, Void, Bitmap> { 
    private Object data; 
    private final WeakReference<ImageView> imageViewReference; 

    public BitmapWorkerTask(ImageView imageView) { 
     imageViewReference = new WeakReference<ImageView>(imageView); 
    } 

    /** 
    * Background processing. 
    */ 
    @Override 
    protected Bitmap doInBackground(Object... params) {    

     data = params[0]; 
     final String dataString = String.valueOf(data); 
     Bitmap bitmap = null; 

     // Wait here if work is paused and the task is not cancelled 
     synchronized (mPauseWorkLock) { 
      while (mPauseWork && !isCancelled()) { 
       try { 
        mPauseWorkLock.wait(); 
       } catch (InterruptedException e) {} 
      } 
     } 

     // If the image cache is available and this task has not been cancelled by another 
     // thread and the ImageView that was originally bound to this task is still bound back 
     // to this task and our "exit early" flag is not set then try and fetch the bitmap from 
     // the cache 
     if (mImageCache != null && !isCancelled() && getAttachedImageView() != null 
       && !mExitTasksEarly) { 
      bitmap = mImageCache.getBitmapFromDiskCache(dataString); 
     } 

     // If the bitmap was not found in the cache and this task has not been cancelled by 
     // another thread and the ImageView that was originally bound to this task is still 
     // bound back to this task and our "exit early" flag is not set, then call the main 
     // process method (as implemented by a subclass) 
     if (bitmap == null && !isCancelled() && getAttachedImageView() != null 
       && !mExitTasksEarly) { 
      bitmap = processBitmap(params[0]); 
     } 

     // If the bitmap was processed and the image cache is available, then add the processed 
     // bitmap to the cache for future use. Note we don't check if the task was cancelled 
     // here, if it was, and the thread is still running, we may as well add the processed 
     // bitmap to our cache as it might be used again in the future 
     if (bitmap != null && mImageCache != null) { 
      mImageCache.addBitmapToCache(dataString, bitmap); 
     }    

     return bitmap; 
    } 

    /** 
    * Once the image is processed, associates it to the imageView 
    */ 
    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     // if cancel was called on this task or the "exit early" flag is set then we're done 
     if (isCancelled() || mExitTasksEarly) { 
      bitmap = null; 
     } 

     final ImageView imageView = getAttachedImageView(); 
     if (bitmap != null && imageView != null) { 
      if (BuildConfig.DEBUG) { 
       Log.d(TAG, "onPostExecute - setting bitmap"); 
      } 
      setImageBitmap(imageView, bitmap); 
     } 
    } 

    @Override 
    protected void onCancelled(Bitmap bitmap) { 
     super.onCancelled(bitmap); 
     synchronized (mPauseWorkLock) { 
      mPauseWorkLock.notifyAll(); 
     } 
    } 

    /** 
    * Returns the ImageView associated with this task as long as the ImageView's task still 
    * points to this task as well. Returns null otherwise. 
    */ 
    private ImageView getAttachedImageView() { 
     final ImageView imageView = imageViewReference.get(); 
     final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); 

     if (this == bitmapWorkerTask) { 
      return imageView; 
     } 

     return null; 
    } 
} 

回答

0

好了,只花了4小时,做了“不可能”,诀窍是:

  1. 测量从HTTP URL加载图像传递到BitmapWorkerTask之前的进度。这link是一个很好的教程。
  2. 将加载URL方法的进度更新传递给BitmapWorkerTask类,但publishProgress()方法只能在AsyncTask内运行,因此要解决该问题,请遵循this
  3. 还有大量的疑难解答,试验和错误,以专门针对您的需求进行定制。

祝你好运!希望这能帮助那里的人开始正确的方向。

0

考虑你如何加载位图

return BitmapFactory.decodeStream(params[0].openConnection().getInputStream()); 

这将是不可能给出一个实际的percentual的进步。对于未定义的进度,你可以通过构造器的进度,并将其保存在另一个WeakReference的,做这样的代码:

// Before start hide the ImageView and show the progressBar 
@Override 
protected void onPreExecute(){ 
    // do the weak reference stuff and call 
    progressBar.setVisibility(View.VISIBLE); 
    imageView.setVisibility(View.INVISIBLE); 
} 

// Once complete, see if ImageView is still around and set bitmap. 
@Override 
protected void onPostExecute(Bitmap bitmap) { 
    // do the weak refence stuff and call 
    progressBar.setVisibility(View.INVISIBLE); 
    imageView.setVisibility(View.VISIBLE); 
} 
+0

我的错我复制了错误的代码,它的编辑应该现在是正确的。对于给您带来的不便和歉意感到抱歉。 – jerrytouille 2013-04-10 13:14:07

+0

我的答案不会随着新代码而改变,我很高兴您使用缓存。 – Budius 2013-04-10 13:16:18

+0

哦,顺便说一句,我已经在同一时间使用'不确定'圆圈进度栏。 – jerrytouille 2013-04-10 13:17:42