继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Android Color 判断色值小结

老菜和尚
关注TA
已关注
手记 185
粉丝 165
获赞 165

   小菜我最近在处理主题色方面的问题,有个小需求是处理更改颜色,判断色值等,稍稍整理了一下。
      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

webp

测试结果

以下是测试完整代码:

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。



作者:老菜和尚


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP