Android 生成随机颜色值

来源:互联网 发布:我给男闺蜜飞机知乎 编辑:程序博客网 时间:2024/06/10 23:41

android rgb随机颜色

只为记录,留着备用

代码

分别取rgb的随机值(0~256),然后加起来就是一个随机颜色值,通过Color.parseColor()转为color值即可使用:

   /**     * 获取十六进制的颜色代码.例如  "#5A6677"     * 分别取R、G、B的随机值,然后加起来即可     *     * @return String     */    public static String getRandColor() {        String R, G, G;        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;    }
原创粉丝点击