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

0

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?

java
android
random
android-animation
asked on Stack Overflow Jul 10, 2018 by UndercoverCoder • edited Jul 10, 2018 by Phantômaxx

3 Answers

1

I don't have an Android project setup here to give it a try, but I would try this out first:

ObjectAnimator is an Animator: java.lang.Object ↳ android.animation.Animator ↳ android.animation.ValueAnimator ↳ android.animation.ObjectAnimator

So you can probably add an Animator.AnimationListener to your ObjectAnimator. Using its addListener(Animator.AnimatorListener listener) method. This listener has an onAnimationEnd() method in which you can call back your fadeLayoutColour method. That should recalculate random values and rerun the animation.

answered on Stack Overflow Jul 10, 2018 by Bentaye • edited Jul 10, 2018 by Bentaye
1
public static void fadeLayoutColour(RelativeLayout rLayout, long timeMills) {
    final ThreadLocalRandom r = ThreadLocalRandom.current();
    final ObjectAnimator colourFade = ObjectAnimator.ofObject(rLayout, "backgroundColor", new ArgbEvaluator(),
            Color.argb(255, r.nextInt(256), r.nextInt(256), r.nextInt(256)),
            Color.argb(255, r.nextInt(256), r.nextInt(256), r.nextInt(256)));
    colourFade.setDuration(timeMills);
    colourFade.setRepeatCount(Animation.INFINITE);
    colourFade.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {}

        @Override
        public void onAnimationEnd(Animator animation) {}

        @Override
        public void onAnimationCancel(Animator animation) {           }

        @Override
        public void onAnimationRepeat(Animator animation) {
            colourFade.setObjectValues(colourFade.getAnimatedValue(),
                    Color.argb(255, r.nextInt(256), r.nextInt(256), r.nextInt(256)));
        }
    });
    colourFade.start();
}
answered on Stack Overflow Jul 10, 2018 by Subzero
0

There is one-liner code to generate the random colour.

new Color((int)(Math.random() * 0x1000000));

If it doesn't change the scope of job you can use above otherwise can use random like this.

int R = (int)(Math.random()*256);
int G = (int)(Math.random()*256);
int B= (int)(Math.random()*256);
Color color = new Color(R, G, B); //random color, but can be bright or dull

//to get rainbow, pastel colors
Random random = new Random();
final float hue = random.nextFloat();
final float saturation = 0.9f;//1.0 for brilliant, 0.0 for dull
final float luminance = 1.0f; //1.0 for brighter, 0.0 for black
color = Color.getHSBColor(hue, saturation, luminance);
answered on Stack Overflow Jul 10, 2018 by Ullas Hunka • edited Jul 10, 2018 by Ullas Hunka

User contributions licensed under CC BY-SA 3.0