首页 文章

了解Android上的颜色(六个字符)

提问于
浏览 313 次
189

我试图了解Android中的颜色是如何工作的 . 我将这个颜色设置为我的 LinearLayout 的背景,并且我得到一个透明的背景灰色:

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

如果我删除最后两个字符(55),我会得到一个纯色,失去透明度 . 我试图找到一个页面,我可以看到有关此的一些解释,但我找不到它 .

7 回答

  • 161

    如果提供6个十六进制数字,则表示RGB(红色,绿色和蓝色的每个值为2个十六进制数字) .

    如果您提供8个十六进制数字,则它是ARGB(分别为alpha,red,green和blue的每个值的2个十六进制数字) .

    所以通过删除最后的55你从A = B4变换,R = 55,G = 55,B = 55(一个大多数是透明的灰色),到R = B4,G = 55,B = 55(完全非 - 透明昏暗的小指) .

    有关支持的格式,请参阅"Color" documentation .

  • 7

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

    Alpha Hex Value Process

    • 将您的不透明度作为十进制值并乘以255.因此,如果您有一个50%不透明的块,则十进制值将为.5 . 例如:.5 x 255 = 127.5

    • 分数不会转换为十六进制,因此您必须将数字向上或向下舍入到最接近的整数 . 例如:127.5轮到128; 55.25轮到55 .

    • 在十进制到十六进制转换器中输入您的小数值,如http://www.binaryhexconverter.com/decimal-to-hex-converter,并转换您的值 .

    • 如果只返回单个值,则在其前面加零 . 例如,如果你正在经历这个过程,你将得到十六进制值D.在它前面添加一个零,使它显示为0D .

    这就是你找到alpha通道值的方法 . 我冒昧地为你整理了一系列 Value 观 . 请享用!

    Hex Opacity Values

    • 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

  • 24

    从@BlondeFurious中得到答案,这里有一些Java代码可以将每个十六进制值从100%变为0%alpha:

    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));
    }
    

    输出:

    100% — FF
    99% — FC
    98% — FA
    97% — F7
    96% — F5
    95% — F2
    94% — F0
    93% — ED
    92% — EB
    91% — E8
    90% — E6
    89% — E3
    88% — E0
    87% — DE
    86% — DB
    85% — D9
    84% — D6
    83% — D4
    82% — D1
    81% — CF
    80% — CC
    79% — C9
    78% — C7
    77% — C4
    76% — C2
    75% — BF
    74% — BD
    73% — BA
    72% — B8
    71% — B5
    70% — B3
    69% — B0
    68% — AD
    67% — AB
    66% — A8
    65% — A6
    64% — A3
    63% — A1
    62% — 9E
    61% — 9C
    60% — 99
    59% — 96
    58% — 94
    57% — 91
    56% — 8F
    55% — 8C
    54% — 8A
    53% — 87
    52% — 85
    51% — 82
    50% — 80
    49% — 7D
    48% — 7A
    47% — 78
    46% — 75
    45% — 73
    44% — 70
    43% — 6E
    42% — 6B
    41% — 69
    40% — 66
    39% — 63
    38% — 61
    37% — 5E
    36% — 5C
    35% — 59
    34% — 57
    33% — 54
    32% — 52
    31% — 4F
    30% — 4D
    29% — 4A
    28% — 47
    27% — 45
    26% — 42
    25% — 40
    24% — 3D
    23% — 3B
    22% — 38
    21% — 36
    20% — 33
    19% — 30
    18% — 2E
    17% — 2B
    16% — 29
    15% — 26
    14% — 24
    13% — 21
    12% — 1F
    11% — 1C
    10% — 1A
    9% — 17
    8% — 14
    7% — 12
    6% — 0F
    5% — 0D
    4% — 0A
    3% — 08
    2% — 05
    1% — 03
    0% — 00
    

    JavaScript版本如下:

    var text = document.getElementById('text');
    for (var i = 1; i >= 0; i -= 0.01) {
        i = Math.round(i * 100) / 100;
        var alpha = Math.round(i * 255);
        var hex = (alpha + 0x10000).toString(16).substr(-2).toUpperCase();
        var perc = Math.round(i * 100);
        text.innerHTML += perc + "% — " + hex + " (" + alpha + ")</br>";
    }
    
    <div id="text"></div>
    

    你也可以只谷歌“数字到十六进制”,其中'数字'是0到255之间的任何值 .

  • -1

    Android Material Design

    这些是用于设置文本颜色不透明度级别的转换 .

    • 100%:FF

    • 87%:DE

    • 70%:B3

    • 54%:8A

    • 50%:80

    • 38%:61

    • 12%:1F

    浅色背景上的暗文本

    enter image description here

    • 主要文字: DE000000

    • 中学文字: 8A000000

    • 禁用文本,提示文本和图标: 61000000

    • 分隔符: 1F000000

    深色背景上的白色文字

    enter image description here

    • 主要文字: FFFFFFFF

    • 中学文字: B3FFFFFF

    • 禁用文本,提示文本和图标: 80FFFFFF

    • 分隔符: 1FFFFFFF

    另见

    • 查找任何百分比here .
  • 1113

    在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

  • 162

    8位十六进制颜色值表示ARGB(Alpha,红色,绿色,蓝色),而6位数值仅假设100%不透明度(完全不透明)并仅定义RGB值 . 因此,要使其完全不透明,您可以使用#FF555555,或只使用#555555 . 每个2位十六进制值是一个字节,表示0-255的值 .

  • 16

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

    例如:

    div{
      background-color:#FF00FFcc;
    }
    

    cc 是不透明度, but old chrome not support that mod

相关问题