Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background resource using Picasso

I know Picasso is an awesome library to play with images.

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

with this code i can load an image to an image view.

But is it possible to set a background resource , using Picasso ?

like image 632
nullUser Avatar asked Jan 11 '14 18:01

nullUser


2 Answers

The Javadoc for Picasso's RequestCreator class has the following example:

public class ProfileView extends FrameLayout implements Target {
    @Override 
    public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
        setBackgroundDrawable(new BitmapDrawable(bitmap));
    }

    @Override public void onBitmapFailed() {
        setBackgroundResource(R.drawable.profile_error);
    }
}
like image 187
gnuf Avatar answered Nov 15 '22 16:11

gnuf


I just had a work around with the Picasso library, I was attempting to set the image as a background as well. Picasso library made it very easy to do this, there is method by name "FIT()" which will do this job for you.

The one magic line from Picasso is

 Picasso.with(context).load(mImageURLS.get(position))
                .fit().placeholder(R.drawable.rtrt).into(mImageDownloader);

.fit() does the trick, thanks.

like image 43
spurthi Avatar answered Nov 15 '22 17:11

spurthi