Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tint Bitmap with Paint?

Tags:

java

android

I'm trying to create a function that tints a Bitmap,

this works...

 imgPaint = new Paint();

    imgPaint.setColorFilter(new LightingColorFilter(color,0));

//when image is being drawn
canvas.drawBitmap(img,matrix,imgPaint);

However, when the bitmap has to be drawn constantly (every frame) , I start to see screen lag, because this didn't occur before the color filter was set, I believe that it is applying the filter every time I need the canvas drawn.

Is there a way to apply the paint once to the bitmap and have it permanently changed?

Any help appreciated :)

like image 841
seveibar Avatar asked Jan 31 '11 21:01

seveibar


1 Answers

Create a second bitmap and draw the first bitmap into it using the color filter. Then use the second bitmap for the high-volume rendering.

EDIT: Per request, here is code that would do this:

public Bitmap makeTintedBitmap(Bitmap src, int color) {
    Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    Canvas c = new Canvas(result);
    Paint paint = new Paint();
    paint.setColorFilter(new LightingColorFilter(color,0));
    c.drawBitmap(src, 0, 0, paint);
    return result;
}

You would then call this method once to convert a bitmap to a tinted bitmap and save the result in an instance variable. You would then use the tinted bitmap directly (without a color filter) in your method that draws to canvas. (It would also be a good idea to pre-allocate the Paint object you will be using in the main draw method and save it in an instance variable as well, rather than allocating a new Paint on every draw.)

like image 143
Ted Hopp Avatar answered Sep 28 '22 04:09

Ted Hopp