How do you tint a bitmap in Android?
Asked Answered
S

1

20

I draw a bitmap onto a canvas using the following call:

_playerImage = BitmapFactory.decodeResource(getResources(), R.drawable.player);

How can I now tint this image white? I'm trying to make the image flash white like in top-scrollers when an enemy is hit by a bullet.

Do I need to use something other than BitmapFactory?

Sarisarid answered 17/8, 2010 at 3:20 Comment(0)
H
46

You can use a ColorFilter on your Paint when you draw the bitmap.

Hessenassau answered 17/8, 2010 at 3:22 Comment(5)
Brilliant! Thanks a ton! "Paint p = new Paint(Color.RED); ColorFilter filter = new LightingColorFilter(Color.RED, 1); p.setColorFilter(filter);"Sarisarid
Hi Romain, is there any method of drawing a tinted bitmap that does not involve creating a new ColorFilter instance for each draw call? Lets say you have couple of 100 sprites that you want to tint in different colors. that would require a new lightingColorFilter for each drawBitmap call. That really does not go along nicely with the whole project butter philosophy of "don't allocate in your render call". Especially since tinting has a direct correlation to the blend/modulate opengl backend which would not require any object being allocated. what about a drawBitmap(..., int mul, int add) call?Solicitude
@Solicitude Good question (and this is really old, but I'm putting this here because it's relevant and others might find it useful)... if you have a relatively small set of colors, you could keep several instances (probably using a pool) of ColorFilter objects, since LightingColorFilter does not seem to support changing its color. If anyone has a better idea, it'd certainly be helpful.Affined
how to fill color over color using paint object?Ways
Note: FoppyOmega's code snippet should be "Paint p = new Paint();". The optional int argument to Paint's constructor is not a color. I got stuck on this for a while because it compiles fine :/.Gametocyte

© 2022 - 2024 — McMap. All rights reserved.