2014-09-18 31 views
5

我想要的是,当我加载我的应用程序时,它随机从预定义的字符串列表中存储一个预定义的字符串列表,存储在名为colors的值xml文件中。Android:设置创建随机颜色背景

我现在所拥有的是一种颜色设置为背景,使用eclipse中的gui编辑器通过字符串颜色代码定义。

因为我的生活不能解决如何获得背景,随机选择9个字符串中的一个,并在每次激活活动时显示它。

对此的指导将是非常宝贵的。

回答

34

在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); 
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 

这种方式,随机数rnd决定的背景是颜色每次onCreate()被调用。

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));

+0

@ Phil3992只是addded定义亲爱的,检查它。 – Kiloreux 2014-09-19 14:16:22

4

没有什么比提供的那些更好的答案。

如果你想要一个真正随机颜色,从res文件中“随机”选择并没有证明几乎是健壮的。

相反,使用此代码片段:

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

我得到价值-118692481300,超出整数? – kemdo 2017-11-20 05:26:08

0

可以使用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 
+0

thanx用于更改格式Vrund Purohit – 2016-09-16 07:38:48