Search code examples
javaandroidbit-manipulation

Manipulate alpha bytes of Java/Android color int


If I have an int in Java that I'm using as an Android color (for drawing on a Canvas), how do I manipulate just the alpha component of that int? For example, how can I use an operation to do this:

int myOpaqueColor = 0xFFFFFF;
float factor = 0;
int myTransparentColor = operationThatChangesAlphaBytes(myOpaqueColor, factor);
//myTransparentColor should now = 0x00FFFFFF;

Ideally, it would be nice to multiply those first bytes by whatever factor is, rather than just set the bytes to a static value.


Solution

  • in the android-support-v4:

    int alpha = 85; //between 0-255
    @ColorInt
    int alphaColor = ColorUtils.setAlphaComponent(Color.Red, alpha);