2013-04-24 110 views
1

我的應用程序使用url加載圖像。我試過使用庫UrlImageViewHelper。有用。但我想添加一個旋轉的進度條。所以我嘗試修改進度條的一部分。 問題是,當我試圖運行我的應用程序時,只有在某些圖像時,進度條纔會出現,然後在已加載圖標時消失。在某些圖像中,它繼續顯示。這是添加我的進度條控件的正確位置嗎?使用進度條加載Android圖像

   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

在這種情況下,我傾向於使用ASyncTask而不是Runnable。 ASyncTask專門爲此目的而設計,包含直接在UI線程上運行的方法(onProgressUpdate()onPreExecute()onPostExecute())。這些方法非常適合根據需要顯示,隱藏和更新進度欄。

這個tutorial應該爲您提供一個相當好的起點。

-1

ASyncTask就是你要找的東西,每當有資源提取或渲染像UI組件和圖像等ASYNCTask就是答案,但是當你在尋找數據提取時,總是使用可運行線程。

類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()執行();

你完成了。