Programmatically make a color more transparent Programmatically make a color more transparent android android

Programmatically make a color more transparent


If you are using support library, you can use:

ColorUtils.setAlphaComponent(int color, int alpha);

If you are not using support library, one-line solution taken from it's source code is:

int res = (color & 0x00ffffff) | (alpha << 24);


Sure...Look at Color and there's a function:

static int   argb(int alpha, int red, int green, int blue)

Return a color-int from alpha, red, green, blue components.

So your RGB values could be static and you just bump the alpha value to get a new transparent version of the color.


Hi there you could use the:

android.support.v4.graphics.ColorUtils#setAlphaComponent

note: the alpha here is from 0 to 255 and not % based.

There are also other util methods such contract and luminosity calculations in there.

Regards