使用进度条加载Android图片

时间:2013-04-24 06:42:14

标签: android urlimageviewhelper

我的应用程序使用url加载图像。我尝试使用库UrlImageViewHelper。有用。但我想添加一个旋转的进度条。所以我尝试修改进度条的一部分。 问题是当我试图运行我的应用程序时,只有在某些图像上会出现进度条,然后在已经加载了iamge时消失。在某些图像中,它继续显示..这是添加进度条控件的正确位置吗?

              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);
      }

如果有人熟悉这个图书馆,你能帮我实现目标吗?感谢

2 个答案:

答案 0 :(得分:0)

在这种情况下,我倾向于使用ASyncTask而不是Runnable。 ASyncTask专门为此目的而设计,包含直接在UI线程(onProgressUpdate()onPreExecute()onPostExecute())上运行的方法。这些方法非常适合根据需要显示,隐藏和更新进度条。

这个tutorial应该为你提供一个相当不错的起点。

答案 1 :(得分:-1)

ASyncTask是您正在寻找的,无论何时有资源获取或呈现像UI组件和图像等等.ASYNCTask是答案,但是当您在寻找数据提取时总是使用Runnable Threads。

类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();

 }
}

然后在Activity代码中执行它,就像这个新的ImageFetch()。execute();

你已经完成了。