Animation does not repeat in android api 8

Asked

Viewed 71 times

0

Hi, I’m creating a simple app and I want to make a light stay flashing, so I added the following code:

private void flashLight() {
    anim = animate(imgLight).setDuration(speeds[speed]);
    anim.alpha(0f).setListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {

            anim.alpha(1f).setListener(new AnimatorListenerAdapter() {

                @Override
                public void onAnimationEnd(Animator animation) {
                        flashLight();
                }
            });
        }
    });
}

The code works perfectly on android devices api 11 or more, but in the versions below it does not work, being that animation only runs once. I wish someone would help me with my problem.

Note: I am using the library Nineoldandroids

From now on I thank you.

  • Silas, why don’t you use the ObjectAnimator instead of the ViewPropertyAnimator? It’s much easier to use transitions (0 to 1) and it still has infinite repetition. I don’t usually use the NineOldAndroids, but I usually use the ObjectAnimator of API 11. If you want I can assemble a response using API 11, believing that the migration to the NineOldAndroids it’s easy.

2 answers

1


Using the ObjectAnimator it is possible to make this animation easier:

ObjectAnimator flashLight = ObjectAnimator.ofFloat(imgLight, View.ALPHA, 0f, 1f);

flashLight.setDuration(speeds[speed]);
flashLight.setRepeatCount(ObjectAnimator.INFINITE);

// Se quiser fazer inverter a animacao ao final,
//flashLight.setRepeatMode(ObjectAnimator.REVERSE);

// Se quiser um Listener para saber quando a animacao se repetiu:
flashLight.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationRepeat(Animator animation) {
        // Animacao se repetiu...
    }
});

flashLight.start();

// Lembrando que para cancelar ou pausar, e preciso usar essa instancia!
// flashLight.cancel();
// Ou
// flashLight.pause();
  • Thanks, you’ve helped a lot!

0

Try this

   AnimationSet animationSet = new AnimationSet(true); 

    AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
    alphaAnimation.setDuration(speeds[speed]);

    animationSet.addAnimation(alphaAnimation);

    imgLight.setAnimation(animationSet);

    alphaAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // Animacao se repetiu...
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // Fim da Animação
        }
    });

    imgLight.startAnimation(animationSet);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.