Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a thumbnail as a placeholder for Picasso

From the UX point of view, it will be great to show the user a thumbnail first until the real image completes loading, then showing it to him, but Picasso uses only a resource file as the place holder like:

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .into(imageView);

So, how can I use a thumbnail URL as the placeholder? , and if I should use Picasso twice, then how?

An issue is already opened on Picasso's github page with this request, but seems it won't be added to Picasso as per JakeWharton. So how could we do it with what's available in hand?

like image 756
AbdelHady Avatar asked Dec 04 '14 17:12

AbdelHady


People also ask

How do you add a placeHolder in Picasso?

Just call . placeHolder() with a reference to a drawable (resource) and Picasso will display that as a placeholder until your actual image is ready. Picasso .

How do you load an image into an imageView from an image URL using Picasso?

Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.


1 Answers

Thanks to raveN here & the comments on the original request on github, finally I've got a working solution:

Picasso.with(context)
       .load(thumb) // thumbnail url goes here
       .into(imageView, new Callback() {
            @Override
            public void onSuccess() {
                Picasso.with(context)
                        .load(url) // image url goes here
                        .placeholder(imageView.getDrawable())
                        .into(imageView);
            }
            @Override
            public void onError() {

            }
        });

The trick here is to get the drawable from the imageView (which is the thumbnail) after the first call & pass it as a placeholder to the second call

-- update --

I've made a blog post describing the whole scenario

like image 92
AbdelHady Avatar answered Oct 26 '22 22:10

AbdelHady