Android ZoneAndroid开发经验谈Android技术知识

Android颜色定义、设置、转换、拾取详解

2018-01-10  本文已影响234人  翻译不了的声响
颜色图

序言

Android中的颜色值通常遵循RGB/ARGB标准,使用时通常以“ # ”字符开头的8位16进制表示。其中ARGB 依次代表透明度(Alpha)、红色(Red)、绿色(Green)、蓝色(Blue),取值范围为0 ~ 255(即16进制的0x00 ~ 0xff)。
A0x000xff表示从透明到不透明,RGB0x000xff表示颜色从浅到深。当RGB全取最小值(0或0x000000)时颜色为黑色,全取最大值(255或0xffffff)时颜色为白色。

透明百分比 十六进制 透明度
100% FF 255
95% F2 242
90% E6 230
85% D9 217
80% CC 204
75% BF 191
70% B3 179
65% A6 166
60% 99 153
55% 8C 140
50% 80 128
45% 73 115
40% 66 102
35% 59 89
30% 4D 77
25% 40 64
20% 33 51
15% 26 38
10% 1A 26
5% 0D 13
0% 00 0

以颜色值#FF99CC00为例,其中FF是透明度,99是红色值,CC是绿色值,00是蓝色值

全透明:#00000000/(0,0,0,0)
半透明:#80000000/(128,0,0,0)
不透明:#FF000000/(255,0,0,0)

正文


下面介绍一下颜色的定义、设置、转换、拾取的几种方式:

1. 颜色定义

(1) Android系统封装好的Color类中的常量

public static final int BLACK = 0xFF000000;
public static final int DKGRAY = 0xFF444444;
public static final int GRAY = 0xFF888888;
public static final int LTGRAY = 0xFFCCCCCC;
public static final int WHITE = 0xFFFFFFFF;
public static final int RED = 0xFFFF0000;
public static final int GREEN = 0xFF00FF00;
public static final int BLUE = 0xFF0000FF;
public static final int YELLOW = 0xFFFFFF00;
public static final int CYAN = 0xFF00FFFF;
public static final int MAGENTA = 0xFFFF00FF;
public static final int TRANSPARENT = 0;

(2) 使用0x开头的颜色值
int color = 0xff00ff00;
(3) 使用Color类的静态方法argb创建颜色
int color = Color.argb(127, 255, 0, 255);
(4) 把16进制颜色值转换为int类型数值
int color = Color.parseColor("#00CCFF");
(5) 使用xml资源文件来表示颜色 (.../res/values/colors.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources> 
<color name="colorPrimary">#3F51B5</color> 
<color name="colorPrimaryDark">#303F9F</color> 
<color name="colorAccent">#FF4081</color> 
</resources>
2. 颜色设置
  android:textColor="@color/text_color_white"          
  android:background="@drawable/full_background" 
3. 颜色转换
 public void toARGB() {
        System.out.println("透明度 | 十六进制");
        System.out.println("---- | ----");
        for (double i = 1; i >= 0; i -= 0.01) {
            i = Math.round(i * 100) / 100.0d;
            int alpha = (int) Math.round(i * 255);
            String hex = Integer.toHexString(alpha).toUpperCase();
            if (hex.length() == 1) hex = "0" + hex;
            int percent = (int) (i * 100);
            System.out.println(String.format("%d%% | %s", percent, hex));
        }
    }
 public static void toRGB(String hex) {
        int color = Integer.parseInt(hex.replace("#", ""), 16);
        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);     
        System.out.println("red="+red+"--green="+green+"--blue="+blue);  
}
public static void toHex(int red, int green, int blue){
        String hr = Integer.toHexString(red);
        String hg = Integer.toHexString(green);
        String hb = Integer.toHexString(blue);
        System.out.println("#"+hr + hg + hb);      
}
4. 颜色拾取

我们一般是无法直观的知道自己需要颜色的值,需要借用取色工具或其他工具去获取相关的颜色值。
(1) 通过QQ等其他的截图工具,可以查看鼠标所在位置的RGB和16进制值
(2) 使用AS自带的吸色工具获取ARGB和16进制值

结语


安卓中的颜色的定义、设置的几种方式,以及颜色的拾取等方法都整理在此,希望可以帮助大家对颜色的相关知识有所了解,方便平时的工作和学习。

上一篇 下一篇

猜你喜欢

热点阅读