Mike Laren
Mike Laren

Reputation: 8178

Setting alpha component for a predefined color

Is there an easy way on Android to set the alpha component of a color retrieved from getResources().getColor(id)?

For example, this is what I'm currently doing to change an existing color to 70% opacity:

DrawerLayout drawerLayout = ...;

int color = getResources().getColor(R.color.bgColor);
color &= 0x00FFFFFF;
color |= (((int) (0.7 * 0xFF)) << 24);

drawerLayout.setScrimColor(color);

Upvotes: 0

Views: 77

Answers (1)

Blackbelt
Blackbelt

Reputation: 157437

using the helper class Color. You can easily retrieve the four components, alpha, red, green, and blue, and set it back with Color.argb, for instance. The class has only static methods, and you don't need to instantiate it. E.g.

int alpha = Color.alpha(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);

int newColor = Color.argb((int)(alpha * 0.7f), red, green, blue);

Here you can find the documentation

Upvotes: 2

Related Questions