How do I set background image with picasso in code

丶灬走出姿态 提交于 2019-11-27 21:55:34
Soham

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.

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;
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!