安卓:在创建时设置随机颜色背景

13

我希望我的应用程序在启动时能够从一个称为colors的values xml文件中预定义的字符串列表中随机选择一个特定的背景颜色。

目前,我使用Eclipse GUI编辑器将一个颜色代码字符串定义为背景颜色。

我不知道如何使背景随机选择9个字符串之一,并在每次活动启动时显示它。

如果能提供指导就非常宝贵了。

6个回答

49

在 colors.xml 文件中

<?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() 中

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

6

这里有一个比提供的答案更好的解决方案。

如果你想要一个真正随机的颜色,从资源文件中“随机”选择并不是最可靠的方法。

相反,使用这段代码片段:

  Random rnd = new Random();
    currentStrokeColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));

我得到了数值-118692481300,超出了整数范围? - kemdo

4

我认为我可以找到一个简单的方法,但实现起来可能会比较长。你可以从预定义的一组颜色中选择随机颜色,然后将该字符串颜色解析为你的背景颜色。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="bright_pink">#FF007F</color>
    <color name="red">#FF0000</color>
    <color name="orange">#FF7F00</color>
    <color name="yellow">#FFFF00</color>
    <color name="chartreuse">#7FFF00</color>
    <color name="green">#00FF00</color>
    <color name="spring_green">#00FF7F</color>
    <color name="cyan">#00FFFF</color>
    <color name="azure">#007FFF</color>
    <color name="blue">#0000FF</color>
    <color name="violet">#7F00FF</color>
    <color name="magenta">#FF00FF</color>
<array name="rainbow">
    <item>@color/bright_pink</item>
    <item>@color/red</item>
    <item>@color/orange</item>
    <item>@color/yellow</item>
    <item>@color/chartreuse</item>
    <item>@color/green</item>
    <item>@color/spring_green</item>
    <item>@color/cyan</item>
    <item>@color/azure</item>
    <item>@color/blue</item>
    <item>@color/violet</item>
    <item>@color/magenta</item>
</array>

接下来是这段Java代码

String[] array = context.getResources().getStringArray(R.array.animals_array);
String randomStr = array[new Random().nextInt(array.length)];

//here you define your layout `

LinearLayout myLayout = (LinearLayout) findViewById(R.id.that_linear);

myLayout.setBackgroundColor(Color.parseColor(randomStr));

1
您可以设置一个变量Random rnd = new Random();作为一个随机数(将生成一个在0和1之间的伪随机数)。然后您可以这样说:
    if (rnd < 0.09) {
        //pick first colour
    } else if (rnd >= 0.09 && rnd < 0.18) {
        //pick second colour
    } else if (rnd >= 0.18 && rnd < 0.27) {
        //pick second colour    
    } else // etc etc up to 1.0 when you will have 9 options, each with an equal chance of randomly being picked

每次调用onCreate()时,随机数rnd将决定背景颜色是哪种颜色。

-1
你可以使用 ColorGenerator 类来获取随机的 color.code
下面是代码片段。
ColorGenerator generator = ColorGenerator.MATERIAL;

int color=generator.getRandomColor();

在这里,您可以使用视图引用来设置颜色,例如

mUserName.setText("Suraj");

mUserName.setTextColor(color);  //it will populate name with random color each time you open your activity

-3
尝试这种简单易行的方法。
private boolean isOne = false, isTwo = false, isThree = false, isFour = false, isFive = false;

if(position==0){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            isTwo = true;
        }else if(isOne){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color1));
            isOne = false;
            isTwo = true;
        }else if(isTwo){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color2));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color2));
            isTwo = false;
            isThree = true;
        }else if(isThree){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color3));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color3));
            isThree = false;
            isFour = true;
        }else if(isFour){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color4));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color4));
            isFour = false;
            isFive = true;
        }else if(isFive){
            holder.llDate.setBackgroundColor(mContext.getResources().getColor(R.color.color5));
            holder.view.setBackgroundColor(mContext.getResources().getColor(R.color.color5));
            isFive = false;
            isOne = true;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接