1

我的应用程序使用 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);
      }

如果有人熟悉这个库,你能帮助实现我的目标吗?谢谢

4

2 回答 2

0

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

教程应该为您提供一个相当好的起点。

于 2013-04-24T06:55:59.027 回答
-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();

 }
}

然后在 Activity 代码中像这样 new ImageFetch().execute();

你完成了。

于 2013-04-24T07:08:21.407 回答