How to add progress bar to Picasso library

若如初见. 提交于 2019-11-29 22:58:37

问题


How can you add a progress bar to Picasso library with this code for downloading photos

String Url = "link url";
Picasso.with(G.currentActivity).load(Url).into(imageView);

回答1:


That feature is not available at the moment. However, you can put a progress bar on top of the imageview and attach a call back for when the image is downloaded then hide the progress bar.

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:gravity="center_vertical">

  <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"
            android:scaleType="center"/>

  <ProgressBar
        style="@android:style/Widget.Holo.Light.ProgressBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>



</RelativeLayout>

then something like this in your activity

import com.squareup.picasso.Callback;

public class MyActivity extends Activity implements Callback {

  private View loaderView;
  private ImageView imageView;

  // ...

  private synchronized void loadImage(Uri uri)
  {
        Picasso.with(this).load(uri)
                .error(R.drawable.ic_error)
                .placeholder(R.drawable.ic_placeholder)
                .resize(getImageWidth(), getImageHeight())

                // passes this object as it's callback when image is loaded
                .centerCrop().into(imageView, this);
  }

  @Override
  public void onSuccess()
  {
    // hide the loader and show the imageview
    loaderView.setVisibility(View.GONE);
    imageView.setVisibility(View.VISIBLE);
  }

  @Override
  public void onError()
  {
    // hide the loader and show the imageview which shows the error icon already
    loaderView.setVisibility(View.GONE);
    imageView.setVisibility(View.VISIBLE);
  }

}



回答2:


There is currently no progress callback in the Picasso library.

According to the author Jake Wharton, this is not likely to be implemented into the library in future and is not easy to implement. According to the feature request:

[Progress Callbacks] would require complicated machinery for very little gain. We recommend that you use an indeterminate progress indicator since the image download should be relatively quick.

I would suggest following the advice of using an indeterminate progress indicator - should your images take a while to download, you may want to investigate if you are either doing too much work on the UI thread before the images are loaded or if the images you are loading are a large file size.




回答3:


We can add a progress bar or otherwise handle callbacks for an image that is loading with:

// Show progress bar
progressBar.setVisibility(View.VISIBLE);
// Hide progress bar on successful load
Picasso.with(this).load(imageUrl)
  .into(imageView, new com.squareup.picasso.Callback() {
      @Override
      public void onSuccess() {
          if (progressBar != null) {
              progressBar.setVisibility(View.GONE);
          }
      }

      @Override
      public void onError() {

      }
});

I find the solution from here



来源:https://stackoverflow.com/questions/32184156/how-to-add-progress-bar-to-picasso-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!