jeudi 15 février 2018

onAnimationRepeat with programatically setting duration for "random" effect...odd behaviour

I have a basic animation file. I want to make the lightning image "flash" at random intervals.

<?xml version="1.0" encoding="utf-8"?>

<alpha
     android:startOffset="500"
    android:fromAlpha="1.0"
    android:toAlpha="0"
    android:repeatCount="infinite"
    android:repeatMode="reverse"

    />

Now the code snippit I have written should on repeat of the animation set the duration to a random number of milliseconds...but all I get is the lightning bolt flashing at high speed (not between the 250-1250ms I expect):

        final Animation lightning1 = AnimationUtils.loadAnimation(c, R.anim.lightning);
    final Random r = new Random();
    final Animation.AnimationListener listenerLightning1 = new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

            int rand = r.nextInt(1000)+250;
            lightning1.setDuration(rand);
        }
    };

    lightning1.setAnimationListener(listenerLightning1);
    lightningBottomRight.startAnimation(lightning1);
}

When I add a manual duration to the animation xml, the code does "pulse" the lightning bolt as expected...for example:

<?xml version="1.0" encoding="utf-8"?>

<alpha
    android:startOffset="500"
    android:fromAlpha="1.0"
    android:toAlpha="0"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:duration="1000"

    />

I am guessing I am not using the listener correctly or as intended but can not find out why.

Thank you for any help!




Aucun commentaire:

Enregistrer un commentaire