2013-04-24 2 views
1

내 응용 프로그램은 URL을 사용하여 이미지를로드합니다. 라이브러리 UrlImageViewHelper을 사용해 보았습니다. 그것은 작동합니다. 하지만 회전 진행 표시 줄을 추가하고 싶습니다. 그래서 나는 progressbar 부분을 수정하려고했습니다. 문제는 응용 프로그램을 실행할 때 일부 이미지에서만 진행 막대가 나타나고 iamge가 이미로드되었을 때 사라지는 문제입니다. 일부 이미지에서 계속 표시됩니다.이게 내 진행률 표시 줄 컨트롤을 추가 할 적절한 위치입니까?안드로이드 이미지로드 progressbar with

   final Runnable completion = new Runnable() { 
       @Override 
       public void run() { 
        assert (Looper.myLooper().equals(Looper.getMainLooper())); 
        Bitmap bitmap = loader.result; 
        Drawable usableResult = null; 
        if (bitmap != null) { 
         usableResult = new ZombieDrawable(url, mResources, bitmap); 
        } 
        if (usableResult == null) { 
         clog("No usable result, defaulting " + url); 
         usableResult = defaultDrawable; 
         mLiveCache.put(url, usableResult); 
        } 
        mPendingDownloads.remove(url); 
    //    mLiveCache.put(url, usableResult); 
        if (callback != null && imageView == null) 
         callback.onLoaded(null, loader.result, url, false); 
        int waitingCount = 0; 
        for (final ImageView iv: downloads) { 
         // validate the url it is waiting for 
         final String pendingUrl = mPendingViews.get(iv); 
         if (!url.equals(pendingUrl)) { 
          clog("Ignoring out of date request to update view for " + url + " " + pendingUrl + " " + iv); 
          continue; 
         } 
         waitingCount++; 
         mPendingViews.remove(iv); 
         if (usableResult != null) { 
    //      System.out.println(String.format("imageView: %dx%d, %dx%d", imageView.getMeasuredWidth(), imageView.getMeasuredHeight(), imageView.getWidth(), imageView.getHeight())); 
          iv.setImageDrawable(usableResult); 
    //      System.out.println(String.format("imageView: %dx%d, %dx%d", imageView.getMeasuredWidth(), imageView.getMeasuredHeight(), imageView.getWidth(), imageView.getHeight())); 
          // onLoaded is called with the loader's result (not what is actually used). null indicates failure. 
         } 
         if (callback != null && iv == imageView) 
          callback.onLoaded(iv, loader.result, url, false); 
        } 
        clog("Populated: " + waitingCount); 

    //    if(imageView.isShown()) 
    //     if(progressBar != null) progressBar.setVisibility(View.GONE); 
       } 
      }; 


      if (file.exists()) { 
       try { 
        if (checkCacheDuration(file, cacheDurationMs)) { 
         clog("File Cache hit on: " + url + ". " + (System.currentTimeMillis() - file.lastModified()) + "ms old."); 

         final AsyncTask<Void, Void, Void> fileloader = new AsyncTask<Void, Void, Void>() { 
          @Override 
          protected Void doInBackground(final Void... params) { 
           loader.onDownloadComplete(null, null, filename); 
           return null; 
          } 
          @Override 
          protected void onPostExecute(final Void result) { 
           completion.run(); 
          } 
         }; 
         executeTask(fileloader); 
         return; 
        } 
        else { 
         clog("File cache has expired. Refreshing."); 
        } 
       } 
       catch (final Exception ex) { 
       } 
      } 

      for (UrlDownloader downloader: mDownloaders) { 
       if (downloader.canDownloadUrl(url)) { 
        downloader.download(context, url, filename, loader, completion); 
        return; 
       } 
      } 

      imageView.setImageDrawable(defaultDrawable); 
    //  if(imageView.isShown()) 
    //   if(progressBar != null) progressBar.setVisibility(View.GONE); 
     } 

이 라이브러리에 익숙한 사람이라면 저의 목표를 달성하는 데 도움이 될 수 있습니까? 감사합니다

답변

0

이 상황에서 나는 Runnable이 아닌 ASyncTask을 사용하려고합니다. ASyncTask는이 목적을 위해 특별히 설계되었으며 UI 스레드 (onProgressUpdate(), onPreExecute()onPostExecute())에서 직접 실행되는 메서드를 포함합니다. 이러한 방법은 필요에 따라 진행률 막대를 표시, 숨김 및 업데이트하는 데 이상적입니다.

tutorial은 상당히 좋은 출발점을 제공해야합니다.

-1

ASyncTask는 리소스 가져 오기 또는 UI 구성 요소 및 이미지 등의 렌더링이있을 때마다 찾고자하는 것입니다. ASYNCTask가 대답이지만 데이터 가져 오기를 원할 때 항상 Runnable 스레드를 사용하십시오.

클래스 ImageFetch은 AsyncTask를 확장 {

private final ProgressDialog dialog = new ProgressDialog(this.context); 
    @Override 
    protected void onPreExecute() { 
      this.dialog.setMessage("Fecthing Image"); 
      this.dialog.setTitle("Please Wait"); 
      this.dialog.setIcon(R.drawable."Any Image here"); 
      this.dialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... voids) { 
     // Put your Image Fetching code here 


    } 
    @Override 
    protected void onPostExecute(Void aVoid) { 
     if (this.dialog.isShowing()) { 
      this.dialog.dismiss(); 

} 
} 

및 활동 코드에서 그 이후

이 새로운 ImageFetch처럼 그것을 할() 실행().;

완료되었습니다.

관련 문제