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