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

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

序言

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

透明百分比十六进制透明度
100%FF255
95%F2242
90%E6230
85%D9217
80%CC204
75%BF191
70%B3179
65%A6166
60%99153
55%8C140
50%80128
45%73115
40%66102
35%5989
30%4D77
25%4064
20%3351
15%2638
10%1A26
5%0D13
0%000

以颜色值#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. 颜色设置
  • java代码中:
    textView.setTextColor(Color.RED);
    textView.setTextColor(0xffff0000);
    textView.setTextColor(Color.argb(127, 255, 0, 255));
    textView.setTextColor(Color.parseColor("#00CCFF"))
    textView.setTextColor(getResources().getColor(R.color.colorPrimary));//已过时
    textView.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));//替换方法

  • xml布局中:

  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));
        }
    }
  • 16进制转GRB颜色值方法:
 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);  
}
  • GRB转16进制颜色值方法:
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进制值

  • 在布局界面,点击左边的小色块

    《Android颜色定义、设置、转换、拾取详解》 取色第一步图.png

  • 在弹出的界面中点击吸管,就可以获取屏幕上任意位置的颜色了

    《Android颜色定义、设置、转换、拾取详解》 取色第二步图.jpg

结语

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

    原文作者:翻译不了的声响
    原文地址: https://www.jianshu.com/p/3c1fe10aed4f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞