Profile: TL; DR: Do not change the RGB value of a color if you want to achieve a gradient from color to transparent.

Tip: if you want to create from one color to transparent gradient (gradient) background, that is about to be careful using the @ android: color/transparent.

This is because when Android draws gradient, it needs to specify two colors to represent both sides of the gradient part (startcolor-centercolor, centercolor-endColor, startcolor-endColor).

Android will calculate the difference between the four components of color (A, R, G, and B). For example, we want to implement a gradient for the following two colors:





When using the @ android: color/transparent, though it does seem to remember is that transparent (A = # 00), but also has its own RGB values. If you look at the Color class in Android.graphics, you’ll notice that color.transparent is equal to 0 (#00000000), which is a completely TRANSPARENT black Color.

Here is an example, we define a gradient as follows:

The display looks like this:

Doesn’t it feel weird. Because we’ve changed a #FF27AE60 green to a completely transparent black.

Therefore, “when we want to achieve a gradient from a color to transparency, do not change the RGB value of the color”. Therefore, the above example should look like this (only changing the Alpha value) :





According to the effect

The gradient process


Zhihu column: Aurora Daily

Android Dev Tip #3

Aurora Daily, aurora developer’s Side Project, reading three foreign technical articles every day, welcome to contribute and follow.