Android RGB与int型颜色互转

来源:互联网 发布:网络上放妻协议 编辑:程序博客网 时间:2024/06/08 14:57

将RGB转化为int

int color=Color.rgb(red, green, blue)  

如果需要透明度

int color=Color.argb(a,red, green, blue)  

将int 转化为 RGB

int color=-4253158;          int red = (color & 0xff0000) >> 16;          int green = (color & 0x00ff00) >> 8;          int blue = (color & 0x0000ff);  

RGB色彩模式分别分红绿蓝三种,他们的颜色取值范围是0~255,

其他的颜色则是通过这三个基本颜色的最大值合成的。255对应16进制是

1111111,再看看代码

int red = (color & 0xff0000) >> 16; 

0xff0000的2进制是:111111110000000000000000(一共24位) 
color & 0xff0000的意思:求和运算,得到一个000000000000000000000000-111111110000000000000000的数值, 
>> 16的意思:右移16位,得到前8位的值,也就是00000000-11111111的数值,对应的2进制也就是0-255的值了。 


代码中,green 和blue 也是上面的道理算出来的。 
这样就得到了rgb的三个值 


原创粉丝点击