I have a png which is mostly transparent, except for a shape I've drawn in white. I want to be able to blend this bitmap with a color when drawing it with Canvas. I'd like something like [Sa, (Dc * (1 - Sa)) + (Sc * C * Sa)], where C would be the color I want to blend with. I assumed I would just set the paint color to C, then call drawBitmap, but the default behavior doesn't seem to be blending.

Is this the right approach? Are there some Xfer codes I'm missing? Can I create a custom shader to do this? Is there an example in the android sdk api I missed that does this?

As an additional constraint, I should mention I want to avoid just creating a new bitmap with the blended color to use. The color C (above) could change every frame, and I don't want to have to generate 100's of these bitmaps for every color that I might need.


1 Answer 1

Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(Color.RED, 0);

Then draw with that Paint object.

Found at "How do you tint a bitmap in Android?"

  • \$\begingroup\$ Thanks! Let me try this and get back to you. Don't think I ever would have found that answer you linked, never referred to it as tinting while doing directX stuff. \$\endgroup\$ Nov 10, 2010 at 3:41
  • \$\begingroup\$ Any particular reason to add "1" to the color value? Also, when they say "multiply", I assume they are multiplying by (channelValue / 255). Is that right? \$\endgroup\$ Jun 6, 2014 at 15:35
  • \$\begingroup\$ Seeing as this answer is four years old, I have no idea. Sorry. I would experiment with it to find out what happens when you change the 1 to a 0 or other values. From looking at documentation and the values of the different Colors, I suspect 0 would be just as effective and more correct for the second parameter. But just a guess. \$\endgroup\$
    – Ricket
    Jun 10, 2014 at 23:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .