mardi 10 juillet 2018

Java - How can I generate new random integers after a time interval?

I'm trying to animate a RelativeLayout cycling through random background colors on Android Studio using the following code:

public static void fadeLayoutColour(RelativeLayout rLayout, long timeMills) {

    // Here are the random values
    int randAlpha = ThreadLocalRandom.current().nextInt(0, 0xff);
    int randRed   = ThreadLocalRandom.current().nextInt(0, 0xff);
    int randGreen = ThreadLocalRandom.current().nextInt(0, 0xff);
    int randBlue  = ThreadLocalRandom.current().nextInt(0, 0xff);
    int randColor = ThreadLocalRandom.current().nextInt(0xff000000, 0xffffffff);

    // The animation
    ObjectAnimator colourFade = ObjectAnimator.ofObject(rLayout,
                                                    "backgroundColor",
                                                    new ArgbEvaluator(),
                                                    Color.argb(randAlpha, randRed, randGreen, randBlue), randColor);
    colourFade.setDuration(timeMills);
    colourFade.setRepeatCount(Animation.INFINITE);
    colourFade.setRepeatMode(ValueAnimator.REVERSE);
    colourFade.start();
}

The issue is that this code selects the random numbers and stops there.

The animation works, but once the random values are selected, they remain as they are. Hence only cycling through two colors, though I want it to cycle through random colors. Which means I have to keep updating the random values.

How can I do this?




Aucun commentaire:

Enregistrer un commentaire