8

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 ?

nullUser
  • 1,159
  • 2
  • 15
  • 26

2 Answers2

12

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);
    }
}
gnuf
  • 2,722
  • 1
  • 25
  • 32
  • 4
    Use setBackground(new BitmapDrawable(context.getResources(), bitmap)) as the code shown in the example is deprecated. – Calvin Aug 05 '14 at 16:17
  • Does what the OP asked for. This should be marked as the answer. @SyamS – Sufian Sep 01 '15 at 11:29
  • This answer is deprecated now as BitmapDrawable is deprecated –  Feb 09 '20 at 17:34
1

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.

Michiel
  • 468
  • 3
  • 23
spurthi
  • 173
  • 1
  • 5
  • 2
    does this even work? `fit()` just re-sizes the image. Doesn't change how it is applied to the target – Vedant Agarwala Oct 15 '15 at 06:46
  • It's not '.fit()' that is doing the "replacement", it's the '.placeholder(Drawable)' that is doing it. Once the image is loaded, it replaces the resource you define as the placeholder. – WallyHale Dec 16 '15 at 17:34
  • This is definitely **not correct answer**. `placeholder` and `fit` are not for changing background color. – Atul Aug 24 '16 at 08:23