简体   繁体   English

在一个时间间隔后如何生成新的随机整数?

[英]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: 我正在尝试使用以下代码为Android Studio上的随机背景颜色设置RelativeLayout循环动画:

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? 我怎样才能做到这一点?

I don't have an Android project setup here to give it a try, but I would try this out first: 我没有在这里进行Android项目设置的尝试,但是我会首先尝试一下:

ObjectAnimator is an Animator : java.lang.Object ↳ android.animation.Animator ↳ android.animation.ValueAnimator ↳ android.animation.ObjectAnimator ObjectAnimator是一个Animatorjava.lang.Object ↳ android.animation.Animator ↳ android.animation.ValueAnimator ↳ android.animation.ObjectAnimator

So you can probably add an Animator.AnimationListener to your ObjectAnimator . 因此,您可以将Animator.AnimationListener添加到ObjectAnimator Using its addListener(Animator.AnimatorListener listener) method. 使用其addListener(Animator.AnimatorListener listener)方法。 This listener has an onAnimationEnd() method in which you can call back your fadeLayoutColour method. 该侦听器有一个onAnimationEnd()方法,您可以在其中调用自己的fadeLayoutColour方法。 That should recalculate random values and rerun the animation. 那应该重新计算随机值并重新运行动画。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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