Tint Bitmap with paint?

I am trying to create a function that colors a bitmap,

it works...

imgPaint = new Paint(); imgPaint.setColorFilter(new LightingColorFilter(color,0)); //when image is being drawn canvas.drawBitmap(img,matrix,imgPaint); 

However, when the bitmap needs to be drawn constantly (every frame), I start to see the screen delay, because it did not happen before the color filter was installed, I believe that it applies the filter every time I need a canvas drawn.

Is there a way to apply paint once to a bitmap and change it forever?

Any help is appreciated :)

+4
source share
1 answer

Create a second raster map and draw the first raster map into it using the color filter. Then use a second raster file to render a large amount.

EDIT: for the request, here is the code that will 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 will then call this method once to convert the bitmap to a tinted bitmap and save the result in an instance variable. Then you would use the tinted bitmap directly (without a color filter) in your method that accesses the canvas . (It would also be nice to pre-select the Paint object that you will use in the main drawing method and save it also in the instance variable, rather than allocating a new Paint for each draw).

+4
source

Source: https://habr.com/ru/post/1337853/


All Articles