安卓系统中的十六进制颜色有时是八位数字。怎么做到的? # FFFFFF 和 # FFFFFF00有什么区别?

我有时在一些例子中看到,Android 中的着色使用 # FF191919。我是说一个八位数的十六进制数字。但它应该只是一个六位数的数字。他们有什么关系?

如果我想把一个六位数字转换成一个八位数字,我该怎么做呢?我是说把 # 424242转换成八位数字着色?细节是什么?

81601 次浏览

The first two characters represent the alpha (transparency) value, where FF is fully visible. This is known as ARGB.

The extra two digits are used to define the colors' transparency, or alpha channel.

Android uses the ARGB format (or AARRGGBB as you use in your example).

For more (Android-specific) information, take a look at the Color documentation.

The eight-digit color is defined with an alpha level.

Let’s extract all. We define the hexadecimal color as six value pairs of RGB two digits per pair.

  • The first two digits for red.
  • The second two digits for green.
  • The third two digits for blue.

Now if you want to set the alpha level of that then it is defined with the eight digits as ARGB.

So now the first two digit values define the alpha and the rest are for the RGB.

The eight-digit hexadecimal value is an ARGB color. It is the same as the usual RGB, but it provides an extra alpha channel.

#RRGGBB in RGB is the same as #00RRGGBB in ARGB. Also take a look at Color.argb.

An eight-digit Android hexadecimal value is called an ARGB. ARGB values are typically expressed using eight hexadecimal digits, with each pair of the hexadecimal digits representing the values of the alpha, red, green and blue channel, respectively. For example, 80FFFF00 represents 50.2% opaque (non-premultiplied) yellow.

The 80 hexadecimal value, which is 128 in decimal, represents a 50.2% alpha value because 128 is approximately 50.2% of the maximum value of 255 (FF hexadecimal); to continue to decipher the 80FFFF00 value, the first FF represents the maximum value red can have; the second FF is like the previous, but for green; the final 00 represents the minimum value blue can have (effectively – no blue).

Consequently red + green yields yellow. In cases where the alpha is not used, this can be shortened to six digits, RRGGBB, and this is why it was chosen to put the alpha in the top bits. Depending on the context, a 0x or a number sign, #, is put before the hexadecimal digits.

Eight-digit hex notation works the same as the six-digit notation, in that you provide a six-digit hexadecimal value, prefixed with a hash (#) symbol.

The difference is, eight-digit notation, as the name suggests, adds two more digits. These two digits represent the alpha channel of the color.

The alpha channel is represented by the last two digits.

This last pair of digits are interpreted as a hexadecimal number (just like the other digits). A value of 00 represents a fully transparent color, and a value of FF represents a fully opaque color.

So for a fully opaque color do this : Color(0xFF<your-6digit-code>)

For example, if you have a 6 digit code: E64526 Now convert it to an 8 digit code with: Color(0xFFE64526)