Search code examples
javaandroidillegalstateexceptionobjectanimator

AnimatorSet: Circular dependencies cannot exist


When i click on start button, then end button and again clicking on start button results in following exception:

10-08 08:57:57.963: E/AndroidRuntime(2009): java.lang.IllegalStateException: Circular dependencies cannot exist in AnimatorSet
10-08 08:57:57.963: E/AndroidRuntime(2009):     at android.animation.AnimatorSet.sortNodes(AnimatorSet.java:848)
10-08 08:57:57.963: E/AndroidRuntime(2009):     at android.animation.AnimatorSet.start(AnimatorSet.java:486)
10-08 08:57:57.963: E/AndroidRuntime(2009):     at com.example.propertyanimation.IndividualPropertyAnimationActivity$1.onClick(IndividualPropertyAnimationActivity.java:51)
10-08 08:57:57.963: E/AndroidRuntime(2009):     at android.view.View.performClick(View.java:4240)

Source code:

    pvhX = PropertyValuesHolder.ofFloat(View.SCALE_X, 2);
    pvhY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 2);
    scaleAnimation = ObjectAnimator.ofPropertyValuesHolder(mSubject, pvhX, pvhY);
    scaleAnimation.setRepeatCount(0);
    scaleAnimation.setRepeatMode(ValueAnimator.REVERSE);
    scaleAnimation.setDuration(3000);

    rotateAnimation = ObjectAnimator.ofFloat(mSubject, View.ROTATION, 360);
    rotateAnimation.setRepeatCount(ValueAnimator.INFINITE);
    rotateAnimation.setDuration(2000);

    mButtonStart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setAniation.play(scaleAnimation).before(rotateAnimation);
            setAniation.start();
        }
    });
    mButtonEnd.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            setAniation.end();
        }
    });

Solution

  • Don't reuse the AnimatorSet, create a new one and it will be ok. The builder is accumulating the values and therefore creating a loop (the animation depending on itself in the "before")