您现在的位置是:首页 > 文章详情

Android Color 判断色值小结

日期:2018-05-24点击:370

      小菜我最近在处理主题色方面的问题,有个小需求是处理更改颜色,判断色值等,稍稍整理了一下。
      Color 大家都很熟悉,其组成方式是 RGB 红绿蓝三原色,小菜觉得可以按 ARGB 即 Alpha 透明度、Red 红色、Green 绿 和 Blue 蓝色来记。默认的 Alpha 为 FF/255 完全不透明,可不设置;若 Alpha 为 00/0 时,代表完全透明,则红绿蓝不起作用;而介于 00-FF/0-255 之间时,可以显示出颜色不同的层次效果。


小菜的测试步骤如下:

  1. 在 color.xml 中定义几个测试颜色值;
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="test_color1">#3F51B5</color> <color name="test_color2">#3F51B5</color> <color name="test_color3">#FF4081</color> <color name="test_color4">#40FF4081</color> </resources> 
  1. 小菜想是否可以直接用 R.color.XX 方式判断色值,测试不相同,小菜理解获取的是 R 的值;
// 日志输出 Log.e("color1==" + R.color.test_color1, "color2==" + R.color.test_color2); // 结果 color1==2131427410: color2==2131427411 
  1. 小菜测试用 getResources().getColor(R.color.XX) 方式,结果是对的;
// 日志输出 Log.e("test_color1==" + getResources().getColor(R.color.test_color1), "test_color2==" + getResources().getColor(R.color.test_color2)); // 结果 test_color1==-12627531: test_color2==-12627531 
  1. 继续测试,获取某个控件背景色;
// 日志输出 if (mColorTv1.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mColorTv1.getBackground(); int color = colordDrawable.getColor(); Log.e("color1==" + color, "test_color3==" + getResources().getColor(R.color.test_color3)); } // 结果 color==-49023: test_color3==-49023 
  1. 获取方式都是可以的,只是这种方式看起来并不直接,转成 16 进制看起来会更自然;
String Color_16(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.alpha(color))); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } String Color_16_NoAlpha(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } 
// 日志输出 if (mColorTv1.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mColorTv1.getBackground(); int color = colordDrawable.getColor(); Log.e("color==" + Color_16(color), "test_color3==" + Color_16(getResources().getColor(R.color.test_color3))); } // 结果 color==#FF4081: test_color3==#FF4081 
测试结果

以下是测试完整代码:

public class ColorActivity extends AppCompatActivity { Toolbar mToolbar; TextView mTitleTv, mColorTv1, mColorTv6; StringBuffer strBuffer = new StringBuffer(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_color); mToolbar = (Toolbar) this.findViewById(R.id.toolbar); mTitleTv = (TextView) this.findViewById(R.id.tv_toolbar_title); mTitleTv.setText("Color 色值判断"); mColorTv1 = (TextView) this.findViewById(R.id.color_tv1); mColorTv6 = (TextView) this.findViewById(R.id.color_tv6); strBuffer.append("R.color.test_color1 方式 " + R.color.test_color1 + "\n" + "R.color.test_color2 方式 " + R.color.test_color2 + "\n"); strBuffer.append("getResources().getColor(R.color.test_color1) 方式 " + getResources().getColor(R.color.test_color1) + "\n" + "getResources().getColor(R.color.test_color2) 方式 " + getResources().getColor(R.color.test_color2) + "\n" + "getResources().getColor(R.color.test_color3) 方式 " + getResources().getColor(R.color.test_color3) + "\n"); if (mToolbar.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mToolbar.getBackground(); int color = colordDrawable.getColor(); Log.e("=======color1==" + color, "=======color2==" + getResources().getColor(R.color.test_color3)); } if (mColorTv1.getBackground() instanceof ColorDrawable) { ColorDrawable colordDrawable = (ColorDrawable) mColorTv1.getBackground(); int color = colordDrawable.getColor(); strBuffer.append("ColorTv " + color + "\n"); strBuffer.append("ColorTv 十六进制 " + Color_16(color) + "\n"); strBuffer.append("ColorTv 十六进制(无透明度) " + Color_16_NoAlpha(color) + "\n"); strBuffer.append("ColorTv 透明度 " + Color_Alpha(color) + " 红 " + Color_Red(color) + " 绿 " + Color_Green(color) + " 蓝 " + Color_Blue(color) + "\n"); } strBuffer.append("R.color.test_color3 十六进制 " + Color_16(getResources().getColor(R.color.test_color3)) + "\n"); strBuffer.append("R.color.test_color3 十六进制(无透明度) " + Color_16_NoAlpha(getResources().getColor(R.color.test_color3)) + "\n"); strBuffer.append("R.color.test_color3 透明度 " + Color_Alpha(getResources().getColor(R.color.test_color3)) + " 红 " + Color_Red(getResources().getColor(R.color.test_color3)) + " 绿 " + Color_Green(getResources().getColor(R.color.test_color3)) + " 蓝 " + Color_Blue(getResources().getColor(R.color.test_color3)) + "\n"); strBuffer.append("R.color.test_color4 十六进制 " + Color_16(getResources().getColor(R.color.test_color4)) + "\n"); strBuffer.append("R.color.test_color4 十六进制(无透明度) " + Color_16_NoAlpha(getResources().getColor(R.color.test_color4)) + "\n"); strBuffer.append("R.color.test_color4 透明度 " + Color_Alpha(getResources().getColor(R.color.test_color4)) + " 红 " + Color_Red(getResources().getColor(R.color.test_color4)) + " 绿 " + Color_Green(getResources().getColor(R.color.test_color4)) + " 蓝 " + Color_Blue(getResources().getColor(R.color.test_color4)) + "\n"); mColorTv6.setText(strBuffer.toString()); } String Color_16(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.alpha(color))); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } String Color_16_NoAlpha(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); stringBuffer.append(Integer.toHexString(Color.red(color))); stringBuffer.append(Integer.toHexString(Color.green(color))); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } String Color_Alpha(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.alpha(color))); return stringBuffer.toString(); } String Color_Red(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.red(color))); return stringBuffer.toString(); } String Color_Green(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.green(color))); return stringBuffer.toString(); } String Color_Blue(int color) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(Integer.toHexString(Color.blue(color))); return stringBuffer.toString(); } public static int Color_ChangeAlpha(int color, int alpha) { int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); } } 

      Tips:获取控件背景色时要注意 backdround 是 color 还是 drawable,可先判断是否是 ColorDrawable。


      下面的是小菜的公众号,欢迎闲来吐槽哦~

小菜公众号
原文链接:https://yq.aliyun.com/articles/633545
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章