How to add progress bar to Picasso library

前端 未结 3 1070
梦谈多话
梦谈多话 2021-01-03 09:48

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).         


        
相关标签:
3条回答
  • 2021-01-03 10:07

    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.

    0 讨论(0)
  • 2021-01-03 10:17

    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);
      }
    
    }
    
    0 讨论(0)
  • 2021-01-03 10:25

    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

    0 讨论(0)
提交回复
热议问题