[toc]
我们在开发过程中经常看到美工给你的标注颜色类似于#0000FF
(蓝色)这样的颜色码,然后还可能有一个不透明度(美工可能给你说的是透明度,但是这个准确来说是不透明度,下面有图为证)
那这个不透明度,在Andriod中怎么表示呢,我来普及一下这个基础知识
Android中颜色简介
Android中的颜色值通常遵循RGB/ARGB标准,使用时通常以#
字符开头,以16进制表示。
lor=’red’>其中RGB依次代表红色(Red)、绿色(Green)、蓝色(Blue).
ARGB依次代表透明度(Alpha)、红色(Red)、绿色(Green)、蓝色(Blue)
eg:#FF00CC99其中FF是透明度,00是红色值,CC是绿色值,99是蓝色值
不透明度
透明度的范围是0-255,在计算机中,我们就用16进制(00-FF)表示,全透明就是00,完全不透明就是FF
透明度和不透明度加起来等于1或者说100%
计算方法
255 不透明度 -> 转换成16进制数
eg:30%不透明度也就是25530%=76.5,四舍五入77,然后利用计算器转为16进制为4D
下面是代码计算1
2
3
4
5
6
7
8
9
10
11public static void main(String[] args) {
for (int i = 0; i <= 100; i++) {
float temp = 255 * i * 1.0f / 100f;
int alpha = Math.round(temp);
String hexStr = Integer.toHexString(alpha);
if (hexStr.length() < 2)
hexStr = "0" + hexStr;
System.out.println(i + "%, " + hexStr.toUpperCase());
}
}
干货(懒程序员必备)
| 不透明度 | 16进制表示 |
| 0% | 00 |
| 1% | 03 |
| 2% | 05 |
| 3% | 08 |
| 4% | 0A |
| 5% | 0D |
| 6% | 0F |
| 7% | 12 |
| 8% | 14 |
| 9% | 17 |
| 10% | 1A |
| 11% | 1C |
| 12% | 1F |
| 13% | 21 |
| 14% | 24 |
| 15% | 26 |
| 16% | 29 |
| 17% | 2B |
| 18% | 2E |
| 19% | 30 |
| 20% | 33 |
| 21% | 36 |
| 22% | 38 |
| 23% | 3B |
| 24% | 3D |
| 25% | 40 |
| 26% | 42 |
| 27% | 45 |
| 28% | 47 |
| 29% | 4A |
| 30% | 4D |
| 31% | 4F |
| 32% | 52 |
| 33% | 54 |
| 34% | 57 |
| 35% | 59 |
| 36% | 5C |
| 37% | 5E |
| 38% | 61 |
| 39% | 63 |
| 40% | 66 |
| 41% | 69 |
| 42% | 6B |
| 43% | 6E |
| 44% | 70 |
| 45% | 73 |
| 46% | 75 |
| 47% | 78 |
| 48% | 7A |
| 49% | 7D |
| 50% | 80 |
| 51% | 82 |
| 52% | 85 |
| 53% | 87 |
| 54% | 8A |
| 55% | 8C |
| 56% | 8F |
| 57% | 91 |
| 58% | 94 |
| 59% | 96 |
| 60% | 99 |
| 61% | 9C |
| 62% | 9E |
| 63% | A1 |
| 64% | A3 |
| 65% | A6 |
| 66% | A8 |
| 67% | AB |
| 68% | AD |
| 69% | B0 |
| 70% | B3 |
| 71% | B5 |
| 72% | B8 |
| 73% | BA |
| 74% | BD |
| 75% | BF |
| 76% | C2 |
| 77% | C4 |
| 78% | C7 |
| 79% | C9 |
| 80% | CC |
| 81% | CF |
| 82% | D1 |
| 83% | D4 |
| 84% | D6 |
| 85% | D9 |
| 86% | DB |
| 87% | DE |
| 88% | E0 |
| 89% | E3 |
| 90% | E6 |
| 91% | E8 |
| 92% | EB |
| 93% | ED |
| 94% | F0 |
| 95% | F2 |
| 96% | F5 |
| 97% | F7 |
| 98% | FA |
| 99% | FC |
| 100% | FF |
eg:
UI给出的颜色值为#000000,不透明度为40%。
那么结合上表,40%不透明度对应的16进制的值为66,那么最终颜色值就是#66000000