代码:
//获取颜色为 如: #fffff 类型
public static String toHexEncoding(int color) {
String R, G, B;
StringBuffer sb = new StringBuffer();
R = Integer.toHexString(Color.red(color));
G = Integer.toHexString(Color.green(color));
B = Integer.toHexString(Color.blue(color));
//判断获取到的R,G,B值的长度 如果长度等于1 给R,G,B值的前边添0
R = R.length() == 1 ? "0" + R : R;
G = G.length() == 1 ? "0" + G : G;
B = B.length() == 1 ? "0" + B : B;
sb.append("#");//0x也可以
sb.append(R);
sb.append(G);
sb.append(B);
return sb.toString();
}
//使用
toHexEncoding(getResources().getColor(R.color.main_color))