0

이미지를 ViewPager에로드 할 수있는 BitmapWorkerTask가 있습니다. 이미지의 로딩 진행 상황을 보여주는 더 나은 UX를 위해 각 페이지에 Horizontal ProgressBar를 추가하려고합니다. (나는 이미 '불확정'원 진행률 표시 줄을 사용하고 있습니다.)Android : Progressbar를 BitmapWorkerTask에 추가하는 방법?

BitmapWorkerTask는 진행률 막대를 추가하기에도 적합한 장소입니까? 그렇다면 어떻게해야합니까? 당신이 실제 percentual 진행 상황을 제공하는 것은 불가능하게 될 비트 맵

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

을로드하는 방법을 고려

/** 
* 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

. 정의되지 않은 ProgressBar의 당신은 생성자 ProgressBar를 전달할 수 있으며, 코드에 다음과 같은 또 다른 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

+0

내 대답은 새 코드로 변경되지 않으며 캐싱을 사용하고있어 기쁘게 생각합니다. – Budius

+0

오 btw 나는 이미 '불확정'원 진행률 표시 줄을 사용하고 있습니다. – jerrytouille

관련 문제