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