开发者

Tint Bitmap with Paint?

开发者 https://www.devze.com 2023-02-07 23:33 出处:网络
I\'m trying to create a function that tints a Bitmap, this works... imgPaint = new Paint(); imgPaint.setColorFilter(new LightingColorFilter(color,0));

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,img开发者_如何学CPaint);

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 :)


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消