49

I want to use Picasso to load three consecutive images one on top of each other in a listview. Using the methods Picasso provides makes this easy. However because these images are loading in at different times it causes a flickering effect as the images come in. For example sometimes image 2 appears before image 1, and when image 1 loads it causes an unnatural stutter. It would be better if I could set the listview's visibility to invisible until all the images are available to be shown. However, there is no callback method I could find for Picasso that would signal when an image has been loaded.

Does anyone know of a solution for this kind of a situation using Picasso?

Thanks

Ebay89
  • 680
  • 1
  • 6
  • 9
  • 1
    @ElectronicGeek I think the way the OP asked this question is fine. He explains what he has already done in the question (he has implemented the image loading but is experiencing flickering) and is asking if Picasso provides some sort of image loading callback to solve the issue. Nothing wrong with that at all. – Alex Lockwood Feb 21 '15 at 20:23

5 Answers5

66

The .into method provides a second argument which is a callback to success and failure. You can use this to keep track of when all three have been called and act on their visibility all at once.

Javadoc: https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html#into-android.widget.ImageView-com.squareup.picasso.Callback-

Olivier Payen
  • 15,198
  • 7
  • 41
  • 70
Jake Wharton
  • 75,598
  • 23
  • 223
  • 230
47

Here is a simple example how to impement Picasso picture loading callback:

Picasso.with(MainActivity.this)
            .load(imageUrl)
            .into(imageView, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {
                            //do smth when picture is loaded successfully

                        }

                        @Override
                        public void onError() {
                            //do smth when there is picture loading error
                        }
                    });

On the latest Picasso's version, onError recives an Exception as parameter and uses get() instead of with()

Picasso.get()
            .load(imageUrl)
            .into(imageView, new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {
                            //do smth when picture is loaded successfully

                        }

                        @Override
                        public void onError(Exception ex) {
                            //do smth when there is picture loading error
                        }
                    });
Gabrielkdc
  • 105
  • 1
  • 11
dzikovskyy
  • 5,027
  • 3
  • 32
  • 43
9

You can implement a callback with Picasso like shown below:

ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().resize(width, height).into(image, new Callback() {
            @Override
            public void onSuccess() {
                layout.setVisibility(View.VISIBLE);
            }

            @Override
            public void onError() {

            }
        });
}

The implementation of my ImageHandler class is shown below:

public class ImageHandler {

    private static Picasso instance;

    public static Picasso getSharedInstance(Context context)
    {
        if(instance == null)
        {
            instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
        }
        return instance;
    }
}
Lord Sidious
  • 359
  • 3
  • 6
3

This is loading a image url into an imageview with simple picasso callbacks

           Picasso.with(this)
            .load(Picurl)
            .into(Imageview, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {


                        }
                    }
            );

And this is picasso image loading with more callbacks

private void loadImage() {
    Picasso.with(this)
            .load(PicURL)
            .into(mContentTarget);
  }


private Target mContentTarget = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    Imageview.setImageBitmap(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
    }
};
MIDHUN CEASAR
  • 131
  • 2
  • 9
0

You could use the Target object. Once target1 receives the callback, you can download the 2nd asset, then get the callback in target2, then trigger the 3rd download.

mbmc
  • 5,024
  • 5
  • 25
  • 53