How to change color of painted bitmaps on canvas?

by jasen_gottlieb , in category: Javascript , 9 months ago

How to change color of painted bitmaps on canvas?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , 9 months ago

@jasen_gottlieb 

To change the color of a painted bitmap on a canvas, you can use the ColorFilter class in Android. Here's how you can do it:

  1. Create a Paint object and set a ColorFilter on it with the desired color.
1
2
3
Paint paint = new Paint();
ColorFilter filter = new LightingColorFilter(Color.RED, Color.TRANSPARENT);
paint.setColorFilter(filter);


  1. Draw the bitmap on the canvas using the Paint object.
1
canvas.drawBitmap(bitmap, x, y, paint);


  1. The bitmap will now be drawn with the color filter applied, changing its color to the specified color.


Alternatively, you can also use the PorterDuff.Mode class to change the color of a bitmap on a canvas. Here's how you can do it:

  1. Create a Paint object and set the desired color filter mode on it.
1
2
Paint paint = new Paint();
paint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN));


  1. Draw the bitmap on the canvas using the Paint object.
1
canvas.drawBitmap(bitmap, x, y, paint);


  1. The bitmap will now be drawn with the specified color filter mode applied, changing its color to the specified color.


By using these methods, you can easily change the color of painted bitmaps on a canvas in Android.

Related Threads:

How to change a color in an area of a canvas?
How to change canvas background color on storybook?
How to change a color of a html element in a canvas?
How to color line in canvas?
How to change the label color in chart.js?
How to change color-scheme on bitbucket?