现在的位置: 首页 > 综合 > 正文

Android图形图画学习(11)——颜色相关

2017年12月17日 ⁄ 综合 ⁄ 共 2055字 ⁄ 字号 评论关闭

Android提供了一个颜色类Color.java提供各种不同的颜色,本节就从Color.java开始探讨颜色在代码中是如何表示的。

Android中使用的是ARGB色彩模式,用于32位位图存储结构。即用32位表示一个像素(总共能表示的色彩数目为2的32次方):其中四个基本元素A,R,G,B各有8位,即各自大小为(0-255),根据各个基本元素的大小搭配组成不同颜色和透明度的色彩。这种表示在BitmapConfig中即为ARGB_8888模式,还有RGB_565模式表示用5个比特位表示R(0-32),6个比特位表示G(0-64),5个比特位表示B(0-32)。

8个比特位大小范围为(0-255,16进制为0x00-0xFF),一个16进制位表示4个二进制位。

/**
 * The Color class defines methods for creating and converting color ints.
 * Colors are represented as packed ints, made up of 4 bytes: alpha, red,
 * green, blue. The values are unpremultiplied, meaning any transparency is
 * stored solely in the alpha component, and not in the color components. The
 * components are stored as follows (alpha << 24) | (red << 16) |
 * (green << 8) | blue. Each component ranges between 0..255 with 0
 * meaning no contribution for that component, and 255 meaning 100%
 * contribution. Thus opaque-black would be 0xFF000000 (100% opaque but
 * no contributions from red, green, or blue), and opaque-white would be
 * 0xFFFFFFFF
 */
public class Color {
    public static final int BLACK       = 0xFF000000;//Color.java中各个颜色的透明度设为了不透明
    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;

    /**
     * Return the alpha component of a color int. This is the same as saying
     * color >>> 24
     */
    //下面的这些方法根据位移操作符提供了获取各个基本元素大小的方法
    public static int alpha(int color) {
        return color >>> 24;                  //无符号右移24位,高位补0。
    }

    /**
     * Return the red component of a color int. This is the same as saying
     * (color >> 16) & 0xFF
     */
    public static int red(int color) {
        return (color >> 16) & 0xFF;         //比如color=Color.CYAN=0xFF00FFFF,先右移16位,高位补0就变成了0x0000FF00,再按位与 0xFF得到0x0000FF00,这样就得到了在该color中的红色配比,青色的红色比重为0
    }

    /**
     * Return the green component of a color int. This is the same as saying
     * (color >> 8) & 0xFF
     */
    public static int green(int color) {
        return (color >> 8) & 0xFF;
    }

    /**
     * Return the blue component of a color int. This is the same as saying
     * color & 0xFF
     */
    public static int blue(int color) {
        return color & 0xFF;
    }

抱歉!评论已关闭.