如何从Java中的一组选定颜色输出随机颜色? (机器人) [英] How to output a random color from a set of selected colors in Java? (Android)

查看:109
本文介绍了如何从Java中的一组选定颜色输出随机颜色? (机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在用户输入答案的任何时候给一个字符串一个随机颜色。我的问题是,我不确定如何使字符串的随机颜色成为特定范围的颜色。例如,如果我希望字符串随机变为蓝色,红色,绿色,粉红色,白色或棕色。只有这些颜色,没有其他颜色。

So I want a string to be given a random color any time the user inputs an answer. My issue is that I'm not sure how to make that random color of a string be a color of a specific range. For example, if I wanted the string to be randomly become blue, red, green, pink, white, or brown. Only these colors, none other.

到目前为止,我使用以下代码管理了所有可能的RBG变体中的完全随机颜色:

So far I have managed a completely random color out of all possible RBG variations using the following code:

Random rand = new Random();
            int r = rand.nextInt(254)+1;
            int g = rand.nextInt(254)+1;
            int b = rand.nextInt(254)+1;

            int randomColor = Color.rgb(r,g,b);
            word.setTextColor(randomColor);

尽管如前所述,这不是我想达到的目标。而不是这个,我想要设置可以随机应用于字符串的颜色。这些是我要选择的颜色,然后随机设置为字符串颜色。这设置了一个我不想要的范围内的完全随机的颜色。我最终会得到5种蓝色变体。

Though as previously mentioned, this is not what I aim to achieve. Instead of this, I want set colors that can be randomly applied to the string. These are colors that I would pick, then have randomly set as the string color. This sets a completely random color out of a range I do not intend to have. I could end up with 5 variations of blue for example.

如果有人能提出解决方案,我会很感激。谢谢。

If anyone could put forward a solution, I'd appreciate it. Thank you.

推荐答案

首先在 color.xml 中定义颜色并创建它的数组。

First of all in color.xml define your colors and create array of it.

<?xml version="1.0" encoding="utf-8"?>
<resources>

<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>

<integer-array name="androidcolors">
    <item>@color/blue</item>
    <item>@color/purple</item>
    <item>@color/green</item>
    <item>@color/orange</item>
    <item>@color/red</item>
    <item>@color/darkblue</item>
    <item>@color/darkpurple</item>
    <item>@color/darkgreen</item>
    <item>@color/darkorange</item>
    <item>@color/darkred</item>
</integer-array>

</resources>

现在在 onCreate 中生成如下所示的随机颜色方法。

Now generate random color like below in onCreate method.

int[] androidColors = getResources().getIntArray(R.array.androidcolors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];

最后设置此生成的颜色。

Lastly set this generated color.

view.setBackgroundColor(randomAndroidColor);  

来自此处

这篇关于如何从Java中的一组选定颜色输出随机颜色? (机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆