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



更多相关文章

  1. Android绘图机制(一)——自定义View的基础属性和方法
  2. Android布局方式(FrameLayout)学习
  3. Android视频播放器实现小窗口和全屏状态切换
  4. Android:修图技术之滤镜效果实现及原理分析——ColorMatrix
  5. Android(安卓)Matrix矩阵原理详解
  6. Android日志:Google官方下拉刷新控件
  7. Android(安卓)View绘制流程(结合源码分析)上
  8. FrameLayout 自定义字母导航条 android:layout_gravity android:
  9. Android自己定义NumberPicker

随机推荐

  1. Android(安卓)手机QQ临时会话
  2. 生成不同ABI版本APK在build.gradle中的配
  3. Android(安卓)View.startAnimation()动画
  4. android 时间戳与日期格式的互相转换 以
  5. android 使用vcard示例
  6. Android使用FFMPEG将H264解码为yuv420p
  7. Speed Up and Back Up Your Rooted Andro
  8. Android(安卓)利用PdfDocument产生PDF文
  9. 如何让TextView自己滚动
  10. android群发短信时判断短信是否发送成功