Animation does not repeat on android api 8

0

Hi, I'm creating a simple application and I want to make a light blink, 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, and animation only runs once. I would like someone to help me with my problem.

Note: I'm using the NineOldAndroids library

Thanks in advance.

    
asked by anonymous 31.07.2014 / 01:34

2 answers

1

Using ObjectAnimator you can do this animation in a simpler way:

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();
    
31.07.2014 / 03:51
0

Try this out

   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);
    
31.07.2014 / 14:01