Android: Set Random colour background on create

我的梦境 提交于 2020-01-09 19:31:50

问题


What I want is when I load my app up it to randomly have a certain colored background from a predefined list of strings stored in a values xml file called colours.

What I currently have is one colour set as the background defined through the string colour code using the gui editor in eclipse.

For the life of me can't work out how to get the background to randomly pick one of the 9 strings and display it each time the activity is activated.

Guidance on this would be invaluable.


回答1:


In 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> 

In onCreate()

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



回答2:


There's a much better answer than the ones provided.

If you want a Truly random color, "randomly" choosing from a res file doesn't prove to be nearly as robust.

Instead, use this code snippet:

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



回答3:


I think I could find an easy approach but some how long to implement ,you choose random color from a defined array of colors and than parse that string color to your background.

<?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>

and than this java code

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



回答4:


You could set a variable Random rnd = new Random(); as a random number (will generate a psuedo-random number between 0 and 1). Then you could say:

    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

This way, the random number rnd will determine which colour the background is each time onCreate() is called.




回答5:


you can use ColorGenerator class to get random color.code.

snippet given below

ColorGenerator generator = ColorGenerator.MATERIAL;

int color=generator.getRandomColor();

here you can use the view reference to set color like

mUserName.setText("Suraj");

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



回答6:


TRY THIS METHOD SIMPLE AND EASY

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


来源:https://stackoverflow.com/questions/25923116/android-set-random-colour-background-on-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!