??????小菜我最近在處理主題色方面的問題,有個小需求是處理更改顏色键菱,判斷色值等谬墙,稍稍整理了一下今布。
??????Color 大家都很熟悉,其組成方式是 RGB 紅綠藍三原色拭抬,小菜覺得可以按 ARGB 即 Alpha 透明度、Red 紅色、Green 綠 和 Blue 藍色來記纯赎。默認(rèn)的 Alpha 為 FF/255 完全不透明哮塞,可不設(shè)置;若 Alpha 為 00/0 時算凿,代表完全透明份蝴,則紅綠藍不起作用;而介于 00-FF/0-255 之間時氓轰,可以顯示出顏色不同的層次效果婚夫。
小菜的測試步驟如下:
- 在 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>
- 小菜想是否可以直接用 R.color.XX 方式判斷色值署鸡,測試不相同案糙,小菜理解獲取的是 R 的值;
// 日志輸出
Log.e("color1==" + R.color.test_color1, "color2==" + R.color.test_color2);
// 結(jié)果
color1==2131427410: color2==2131427411
- 小菜測試用 getResources().getColor(R.color.XX) 方式靴庆,結(jié)果是對的时捌;
// 日志輸出
Log.e("test_color1==" + getResources().getColor(R.color.test_color1), "test_color2==" + getResources().getColor(R.color.test_color2));
// 結(jié)果
test_color1==-12627531: test_color2==-12627531
- 繼續(xù)測試,獲取某個控件背景色炉抒;
// 日志輸出
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));
}
// 結(jié)果
color==-49023: test_color3==-49023
- 獲取方式都是可以的奢讨,只是這種方式看起來并不直接,轉(zhuǎn)成 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)));
}
// 結(jié)果
color==#FF4081: test_color3==#FF4081
測試結(jié)果
以下是測試完整代碼:
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。
來源: 阿策小和尚