28

I know picasso loads image into imageview etc but how do I set my layout background image using picasso?

My code:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout);
        relativeLayout.setBackgroundResource(R.drawable.table_background);
        Picasso.with(MainActivity.this)
                .load(R.drawable.table_background)
                .resize(200, 200)
                .into(relativeLayout);
        return relativeLayout;
    }

What I have here gives any error saying it cannot be resolved. I have a ScrollView and relative layouts.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Toye
  • 483
  • 1
  • 4
  • 7

2 Answers2

66

Use callback of Picasso

    Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){

  @Override
  public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
     mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
  }

  @Override
  public void onBitmapFailed(final Drawable errorDrawable) {
      Log.d("TAG", "FAILED");
  }

  @Override
  public void onPrepareLoad(final Drawable placeHolderDrawable) {
      Log.d("TAG", "Prepare Load");
  }      
})

UPDATE:

Please check this also .As @OlivierH mentioned in the comment.

Community
  • 1
  • 1
Soham
  • 4,397
  • 11
  • 43
  • 71
  • 2
    Be careful : as mentioned in [this answer](http://stackoverflow.com/a/31464960/2806497), `Picasso holds a weak reference to Target objects, hence it is likely to be garbage collected.` – OlivierH Feb 01 '16 at 13:38
  • 1
    @OlivierH .I have updated the answer.Thanks for pointing it out. – Soham Feb 02 '16 at 04:48
  • Another good solution is to store the target in the view tag as a strong reference. I fount the solution here and this works for me: http://stackoverflow.com/questions/24180805/onbitmaploaded-of-target-object-not-called-on-first-load#answers – user2331454 Feb 04 '16 at 12:36
  • What is mainLayout? – Fernando Torres Apr 29 '19 at 04:32
  • @FernandoUrban it's the layout where you want to set the bitmap. – Soham Apr 30 '19 at 05:40
  • Answer is deprecated now –  Feb 09 '20 at 17:36
0

The best way it's create custom Transformation for example for fill color:

public class BackgroundColorTransform implements Transformation {

    @ColorInt int mFillColor;

    public BackgroundColorTransform(@ColorInt int color) {
        super();
        mFillColor = color;
    }

    @Override
    public Bitmap transform(Bitmap bitmap) {
        // Create another bitmap that will hold the results of the filter.
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap out = Bitmap.createBitmap(width, height, bitmap.getConfig());
        Canvas canvas = new Canvas(out);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        canvas.drawColor(mFillColor);
        canvas.drawBitmap(bitmap, 0f, 0f, paint);
        bitmap.recycle();
        return out;
    }

    @Override
    public String key() {
        return "BackgroundColorTransform:"+mFillColor;
    }

}

Usage:

mPicasso.load(imageUrl)
               .transform(new BackgroundColorTransform(ContextCompat.getColor(getContext(),R.color.black)))
                .into(mLogoImageView);

If you want to add a vectorDrawable image use the Transformation :

public class AddVectorDrawableTransform implements Transformation {

    private Drawable mDrawable;
    @ColorInt int mTintColor;

    public AddVectorDrawableTransform(Drawable drawable, @ColorInt int tintColor) {
        super();
        mDrawable = drawable;
        mTintColor = tintColor;
    }

    @Override
    public Bitmap transform(Bitmap bitmap) {
        // Create another bitmap that will hold the results of the filter.
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap out = Bitmap.createBitmap(width, height, bitmap.getConfig());
        Canvas canvas = new Canvas(out);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        canvas.drawBitmap(bitmap, 0f, 0f, paint);
        Drawable drawable = mDrawable.mutate();
        drawable = DrawableCompat.wrap(drawable);
        DrawableCompat.setTint(drawable, mTintColor);
        DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_IN);
//        mDrawable.setColorFilter( mTintColor, PorterDuff.Mode.MULTIPLY);
        drawable.setBounds(width/4, height/4, 3*width/4, 3*height/4);
        drawable.draw(canvas);
        bitmap.recycle();
        return out;
    }

    @Override
    public String key() {
        return "AddDrawableTransform:"+mDrawable+", "+mTintColor;
    }

}
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138