android rgb随机颜色
只为记录,留着备用
代码
分别取rgb的随机值(0~256),然后加起来就是一个随机颜色值,通过Color.parseColor()转为color值即可使用:
/**
* 获取十六进制的颜色代码.例如 "#5A6677"
* 分别取R、G、B的随机值,然后加起来即可
*
* @return String
*/
public static String getRandColor() {
String R, G, B;
Random random = new Random();
R = Integer.toHexString(random.nextInt(256)).toUpperCase();
G = Integer.toHexString(random.nextInt(256)).toUpperCase();
B = Integer.toHexString(random.nextInt(256)).toUpperCase();
R = R.length() == 1 ? "0" + R : R;
G = G.length() == 1 ? "0" + G : G;
B = B.length() == 1 ? "0" + B : B;
return "#" + R + G + B;
}