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?

FoppyOmega
  • 447
  • 1
  • 4
  • 17

1 Answers1

46

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Romain Guy
  • 97,993
  • 18
  • 219
  • 200
  • 8
    Brilliant! Thanks a ton! "Paint p = new Paint(Color.RED); ColorFilter filter = new LightingColorFilter(Color.RED, 1); p.setColorFilter(filter);" – FoppyOmega Aug 17 '10 at 23:05
  • 1
    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? – P.Melch Jul 15 '12 at 16:23
  • 2
    @P.Melch 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. – kungphu Mar 07 '14 at 22:20
  • how to fill color over color using paint object? – jack Sep 20 '14 at 06:18
  • 2
    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 :/. – stewbasic Dec 08 '14 at 06:50