我试图了解颜色在Android中是如何工作的。我有这个颜色设置为我的线性布局的背景,我得到了一个灰色的背景和一些透明度:

<gradient android:startColor="#b4555555" android:endColor="#b4555555"
 android:angle="270.0" />

如果我删除最后两个字符(55),我得到一个纯色,失去透明度。我试图找到一个页面,在那里我可以看到一些关于这个的解释,但我找不到。


当前回答

在新的chrome版本(可能67.0.3396.62),CSS十六进制颜色可以使用这个模型显示,

eg:

div{
  background-color:#FF00FFcc;
}

Cc是不透明度,但旧chrome不支持该mod

其他回答

Android使用十六进制的ARGB值,格式化为#AARRGGBB。第一对字母AA代表alpha通道。必须将十进制不透明度值转换为十六进制值。以下是步骤:

Alpha十六进制数值过程

Take your opacity as a decimal value and multiply it by 255. So, if you have a block that is 50% opaque the decimal value would be .5. For example: .5 x 255 = 127.5 The fraction won't convert to hexadecimal, so you must round your number up or down to the nearest whole number. For example: 127.5 rounds up to 128; 55.25 rounds down to 55. Enter your decimal value in a decimal-to-hexadecimal converter, like http://www.binaryhexconverter.com/decimal-to-hex-converter, and convert your values. If you only get back a single value, prefix it with a zero. For example, if you're trying to get 5% opacity and you're going through this process, you'll end up with the hexadecimal value of D. Add a zero in front of it so it appears as 0D.

这就是找到alpha通道值的方法。我冒昧地给你列了一份价值观清单。享受吧!

十六进制不透明度值

100% - ff 95% - f2 90% - e6 85% - d9 80% - cc 75% - bf 70% - b3 65% - a6 60% - 99 55% - 8c 50% - 80 45% - 73 40% - 66 35% - 59 30% - 4d 25% - 40 20% - 33 15% - 26 10% - 1a 5% - 0d 0% - 00

在Android上,颜色可以用以下格式声明

#AARRGGBB

AA -是我们最感兴趣的部分,它代表alpha通道

RR GG BB -红色,绿色和蓝色通道分别

现在为了给我们的颜色增加透明度,我们需要在它前面加上表示alpha(透明度)的十六进制值。

例如,如果你想设置80%的透明度值为黑色(#000000),你需要在它前面加上CC,结果我们最终得到了下面的颜色资源#CC000000。

你可以在我的博客上读到更多细节 https://androidexplained.github.io/android/ui/2020/10/12/hex-color-code-transparency.html

使用这个表(我更喜欢把它放在colors.xml中,以便快速搜索)

<!--Percent to hex conversion table-->
<!--%  0  1  2  3  4  5  6  7  8  9-->
<!--0 00 03 05 08 0A 0D 0F 12 14 17-->
<!--1 19 1C 1F 21 24 26 29 2B 2E 30-->
<!--2 33 36 38 3B 3D 40 42 45 47 4A-->
<!--3 4D 4F 52 54 57 59 5C 5E 61 63-->
<!--4 66 69 6B 6E 70 73 75 78 7A 7D-->
<!--5 80 82 85 87 8A 8C 8F 91 94 96-->
<!--6 99 9C 9E A1 A3 A6 A8 AB AD B0-->
<!--7 B3 B5 B8 BA BD BF C2 C4 C7 C9-->
<!--8 CC CF D1 D4 D6 D9 DB DE E0 E3-->
<!--9 E6 E8 EB ED F0 F2 F5 F7 FA FC-->

在Android上,颜色可以指定为RGB或ARGB。

http://en.wikipedia.org/wiki/ARGB

在RGB中,每种颜色(红、绿、蓝)都有两个字符,在ARGB中,alpha通道有两个额外的字符。

如果你有8个字符,它是ARGB,前两个字符指定alpha通道。如果你去掉前面的两个字符,它只是RGB(纯色,没有alpha/透明度)。如果你想在你的Java源代码中指定一种颜色,你必须使用:

int Color.argb (int alpha, int red, int green, int blue)

alpha  Alpha component [0..255] of the color
red    Red component [0..255] of the color
green  Green component [0..255] of the color
blue   Blue component [0..255] of the color

参考:argb

在新的chrome版本(可能67.0.3396.62),CSS十六进制颜色可以使用这个模型显示,

eg:

div{
  background-color:#FF00FFcc;
}

Cc是不透明度,但旧chrome不支持该mod