android 资源之 color
文章出处:https://blog.csdn.net/shift_wwx/article/details/48262465
请转载的朋友标明出处~~
前言:对于color 资深的Android 工程师肯定都熟悉,我是记忆力太差了,随记一下。
红、绿、蓝三个值是就是代表颜色的取值,而Alpha代表的是透明度。最低值为0,表示颜色完全透明,而此时RGB是什么取值都不重要了。Alpha最高可取值为255,表示颜色完全不透明。如果需要颜色透明、半透明,那么可以取值0-255中间的一些值,这常常用在前端图层绘制时。
下面我大概列了两个方面理解color:
一、Color 类
在android中有个Color 类,路径大概在frameworks/base/graphics/java/android/graphics/下。里面定义了一下 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;需要注意的是 transparent,就是alpha 为0,rgb是完全不关心的。Color 类中还提供了其他的一些方法:
public static int alpha(int color) {return color >>> 24;}/*** 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;}/*** 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;}/*** Return a color-int from red, green, blue components.* The alpha component is implicity 255 (fully opaque).* These component values should be [0..255], but there is no* range check performed, so if they are out of range, the* returned color is undefined.* @param red Red component [0..255] of the color* @param green Green component [0..255] of the color* @param blue Blue component [0..255] of the color*/public static int rgb(int red, int green, int blue) {return (0xFF << 24) | (red << 16) | (green << 8) | blue;}/*** Return a color-int from alpha, red, green, blue components.* These component values should be [0..255], but there is no* range check performed, so if they are out of range, the* returned color is undefined.* @param alpha Alpha component [0..255] of the color* @param red Red component [0..255] of the color* @param green Green component [0..255] of the color* @param blue Blue component [0..255] of the color*/public static int argb(int alpha, int red, int green, int blue) {return (alpha << 24) | (red << 16) | (green << 8) | blue;}/*** Returns the hue component of a color int.* * @return A value between 0.0f and 1.0f* * @hide Pending API council*/public static float hue(int color) {int r = (color >> 16) & 0xFF;int g = (color >> 8) & 0xFF;int b = color & 0xFF;int V = Math.max(b, Math.max(r, g));int temp = Math.min(b, Math.min(r, g));float H;if (V == temp) {H = 0;} else {final float vtemp = (float) (V - temp);final float cr = (V - r) / vtemp;final float cg = (V - g) / vtemp;final float cb = (V - b) / vtemp;if (r == V) {H = cb - cg;} else if (g == V) {H = 2 + cr - cb;} else {H = 4 + cg - cr;}H /= 6.f;if (H < 0) {H++;}}return H;}/*** Returns the saturation component of a color int.* * @return A value between 0.0f and 1.0f* * @hide Pending API council*/public static float saturation(int color) {int r = (color >> 16) & 0xFF;int g = (color >> 8) & 0xFF;int b = color & 0xFF;int V = Math.max(b, Math.max(r, g));int temp = Math.min(b, Math.min(r, g));float S;if (V == temp) {S = 0;} else {S = (V - temp) / (float) V;}return S;}/*** Returns the brightness component of a color int.** @return A value between 0.0f and 1.0f** @hide Pending API council*/public static float brightness(int color) {int r = (color >> 16) & 0xFF;int g = (color >> 8) & 0xFF;int b = color & 0xFF;int V = Math.max(b, Math.max(r, g));return (V / 255.f);}可以获取 int alpha,int red,int green, int blue;也可以通过分别传入r、g、b获取最终的argb,或者是传入a、r、g、b 获取最终的argb;
还可以通过传入的rgb 获取色度(h)、饱和度(s)、亮度(b);
还有一些rgb 和 hsb之间的转换等等。
例如:
int color = Color.rgb(100, 200, 255);这个时候alpha 是FF。int color = Color.argb(100, 100, 200, 255);加了透明色的color。但是一般这些如果在code中写死的话,项目的兼容性就不怎么好,还是在资源用配置比较好,项目之间可以通过overlay 来继承。
二、应用中的color
可以在values中new 一个color.xml:
#1E1E1E #FFFFFF #5D5D5D #1E1E1E #1E1E1E
这样可以在code中用:int color = getResources().getColor(R.color.source_unfocus);也可以直接在资源的xml中用;@color/source_unfocus当然一些View中color 也是可以动态变的。例如:
当然啦,也可以用shape,详细看一下:《android资源文件之:shape详解》本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
