简体   繁体   中英

Randomize colors in array on button click

I am trying to Randomize the colors automatically on a single button click. I am able to randomize and display the background color, but I have to click the button each time to get a different color. I am trying to click the button once and it automatically loop through the array and display the colors automatically. I know I need some form of loop around the array, but I have no clue where to put it.

private View windowView;
private Button clickMe;
private int[colors];

colors=new int[]{Color.CYAN, Color.GREEN, Color.RED};

for (int i = 0; i < colors.length; i++) {
                Random random = new Random();
                int randomNum = random.nextInt(colorArrayLength);

                windowView.setBackgroundColor(colors[randomNum]);
            }

I do not understand why this is not looping through the array. Any hints and assistance would be much appreciated.

I believe you are randomly selecting the color and setting it in the background. However as it is inside a loop, it is changing so fast that the change is not notable in the visual rendering. You need to pause few milliseconds to see the change visually.

You may try out out the following:

private View windowView;
private Button clickMe;
private int[colors];

colors=new int[]{Color.CYAN, Color.GREEN, Color.RED};

final int[] finalColors = colors;
final View finalWindowView = windowView;

for (int i = 0; i < finalColors.length; i++) {
    Random random = new Random();

    final int randomNum = random.nextInt(finalColors.length);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // Do after 500ms
            finalWindowView.setBackgroundColor(colors[randomNum]);
        }
    }, 500 * i);

}

Try out this it will helpful to you

private int getMatColor(String typeColor)
{
    int returnColor = Color.BLACK;
    int arrayId = getResources().getIdentifier("mdcolor_" + typeColor, "array", getApplicationContext().getPackageName());

    if (arrayId != 0)
    {
        TypedArray colors = getResources().obtainTypedArray(arrayId);
        int index = (int) (Math.random() * colors.length());
        returnColor = colors.getColor(index, Color.BLACK);
        colors.recycle();
    }
    return returnColor;
}

Your code isn't really randomizing an array, but simply picking a random color from your array.

There's a slight difference in this. Randomizing an array means the order of every item in your array becomes randomized. There is no way for repeats to be possible for this.

Whereas, randomly selecting a color from your array might make it possible for you to have the same color selected.

What you're doing is the latter and it's doing that task properly.

Therefore, your code isn't considered looping through an array. You're simply selecting a random color from the array for n number of times, with n being the size of your array.

Additionally, the colors you're randomly picking aren't displaying to windowView because the colors are being changed immediately.

For example, your for loop might turn windowView to CYAN, but immediately afterwards the loop will change it to RED. Thus, it'll only display the very last random value that is selected from your array and you won't see any of the previous color changes.

If you want to see the changes, you need to add a delay between each random color change.

What's colorArrayLength ? no this variable. Codes like below are run normally.

        for (int i = 0; i < colors.length; i++) {
            Random random = new Random();
            int randomNum = random.nextInt(colors.length);

            button.setBackgroundColor(colors[randomNum]);
        }

But even this, the view's background color is the last color after loop, you can not see the result of color looping effect. If you want to see the process of color changing, maybe add Timer.schedule() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM