rosu alin
rosu alin

Reputation: 5830

How to change color of a imageview that I load from Picasso from Green to Red

This is my code:

backPic.setScaleType(ImageView.ScaleType.FIT_CENTER);
backPic.setColorFilter(ContextCompat.getColor(context, R.color.red), android.graphics.PorterDuff.Mode.MULTIPLY);
Picasso.with(context).load(icon).into(backPic, new com.squareup.picasso.Callback() {
    @Override
    public void onSuccess() {
    }

    @Override
    public void onError() {
        UserVehicle.setVehicleClassPic(getVclass().getId(), backPic);
    }
});

The initial picture is this:

enter image description here

This is what I get now:

enter image description here

How can I make it so the bike in the second picture is red, not gray towards green?

The color I sent to the image filter is red.

Upvotes: 0

Views: 1619

Answers (2)

rosu alin
rosu alin

Reputation: 5830

Using the comment from André Sousa I did this code:

   Picasso.with(context).load(icon).into(backPic, new com.squareup.picasso.Callback() {
                @Override
                public void onSuccess() {
                    DrawableCompat.setTint(backPic.getDrawable(), context.getResources().getColor(R.color.red));
                }

                @Override
                public void onError() {
                    UserVehicle.setVehicleClassPic(getVclass().getId(), backPic);
                }
            });

So it would set the tint after the Picasso image has loaded, which works

Upvotes: 3

Technivorous
Technivorous

Reputation: 1712

please read here:

the problem is this line:

  backPic.setColorFilter(ContextCompat.getColor(context, R.color.red), 
  android.graphics.PorterDuff.Mode.MULTIPLY);

specifically:

  PorterDuff.Mode.MULTIPLY

you want:

 PorterDuff.Mode.DST

i believe. but if you read the docs i provided you will see what the different modes do. just remember when you math colors they do funny things. multiplying green and red is giving you that funky grey, the ADD mode would have been my first choice, but that will give you a funk brown. DST replaces the old pixel with the new ones, keeping it from mixing colors.

good luck!

Upvotes: 0

Related Questions