Bartos
Bartos

Reputation: 1037

Android | ValueAnimator is not working after restart

In my app I'm using ValueAnimator - it fades one image into another. It works perfectly but only after launching app. When I want to animate images again animator is not working. I tried do use animation.end() in onAnimationEnd listener but app crashes. I tried everything. Can you please help me ? Here's my method to start animation

private void animate() {
        if(animator != null)
        animator.end();
        animator = ValueAnimator.ofFloat(0f, 1f);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mPackOneSecond.setAlpha((Float) animation.getAnimatedValue());
                mPackTwoSecond.setAlpha((Float) animation.getAnimatedValue());
                mPackThreeSecond.setAlpha((Float) animation.getAnimatedValue());
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                startSorting(option);

            }
        });

        animator.setDuration(2500);
        animator.start();
    }

Basically animate() in onCreate() works perfectly but when I want to restart it - it doesnt. Why ?

Upvotes: 1

Views: 733

Answers (1)

ucMax
ucMax

Reputation: 5448

Call animator.cancel() before restart.

Upvotes: 1

Related Questions