Android之Dedug--Circular dependencies cannot exist in AnimatorSet,circular
今日,在學習AnimatorSet時,使用play、with、after、before時,代碼書寫如下:
ObjectAnimator animator1 = ObjectAnimator.ofFloat(v,"translationX",300f);ObjectAnimator animator2 = ObjectAnimator.ofFloat(v,"scaleX",1f,0f,1f);ObjectAnimator animator3 = ObjectAnimator.ofFloat(v,"scaleY",1f,0f,1f);AnimatorSet set = new AnimatorSet();set.setDuration(1000);set.play(animator1).after(animator2).before(animator3).with(animator2);set.start();
運行代碼時,報錯,錯誤報表如下:
12-12 16:26:18.210 28528-28528/com.zhangmiao.animationdemo E/AndroidRuntime: FATAL EXCEPTION: main Process: com.zhangmiao.animationdemo, PID: 28528 java.lang.IllegalStateException: Circular dependencies cannot exist in AnimatorSet at android.animation.AnimatorSet.sortNodes(AnimatorSet.java:921) at android.animation.AnimatorSet.start(AnimatorSet.java:539) at com.zhangmiao.animationdemo.ObjectAnimatorDemo.onClick(ObjectAnimatorDemo.java:82) at android.view.View.performClick(View.java:4868) at android.view.View$PerformClick.run(View.java:20294) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:192) at android.app.ActivityThread.main(ActivityThread.java:5718) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:975) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
然後,我將set.play(animator1).after(animator2).before(animator3).with(animator2);代碼修改為set.play(animator1);這時並不會報錯,我依次添加動畫,當添加到set.play(animator1).after(animator2).before(animator3).with(animator2);時,才會報錯,所以是最後一個with(animator2)有問題,然後,我又添加了一個動畫
ObjectAnimator animator4 = ObjectAnimator.ofFloat(v,"scaleY",1f,0f,1f);
將set.play(animator1).after(animator2).before(animator3).with(animator2);修改為
set.play(animator1).after(animator2).before(animator3).with(animator4);
代碼就不會報錯了。
所以,得到的結論是:
AnimatorSet的play、with、before、after函數不可複用同一個Animator,如果需要使用同樣的Animator,可以建立多個相同動畫的Animator,然後分別使用這多個動畫即可。