標籤:
<span style="font-size:18px;"> </span>
<span style="font-size:18px;"> 最近在用<a target=_blank href="https://github.com/skyfishjy/android-ripple-background">https://github.com/skyfishjy/android-ripple-background</a>開原始檔控制的時候,在Android4.0.x上遇到一個問題,本來優雅的動畫變得很快。</span>
<span style="font-size:18px;"></span>
<span style="font-size:18px;"> 剛開始以為是硬體加速的問題,但是查資料後,發現google在4.0的時候就預設的把硬體加速開啟了,所以之上的版本都應該一樣的。後來和同事一起研究的時候發現,好像是animatorSet.setDuration()方法沒有效果,又查了源碼發現下面的區別:</span>
API 15
/** * Sets the length of each of the current child animations of this AnimatorSet. By default, * each child animation will use its own duration. If the duration is set on the AnimatorSet, * then each child animation inherits this duration. * * @param duration The length of the animation, in milliseconds, of each of the child * animations of this AnimatorSet. */ @Override public AnimatorSet setDuration(long duration) { if (duration < 0) { throw new IllegalArgumentException("duration must be a value of zero or greater"); } // Just record the value for now - it will be used later when the AnimatorSet starts mDuration = duration; return this; }
API 19
/** * Sets the length of each of the current child animations of this AnimatorSet. By default, * each child animation will use its own duration. If the duration is set on the AnimatorSet, * then each child animation inherits this duration. * * @param duration The length of the animation, in milliseconds, of each of the child * animations of this AnimatorSet. */ @Override public AnimatorSet setDuration(long duration) { if (duration < 0) { throw new IllegalArgumentException("duration must be a value of zero or greater"); } for (Node node : mNodes) { // TODO: don't set the duration of the timing-only nodes created by AnimatorSet to // insert "play-after" delays node.animation.setDuration(duration); } mDuration = duration; return this; }
原來是在API15上AnimatorSet.setDuration()方法並沒有給它的子objectAnimator設定,解決方案當然是給每個add到animatorSet中的ObjectAnimator都設定自己的duration。
這是最典型的Android版本相容問題,踩過坑了就不要再犯。
android 4.0.x上AnimatorSet.setDuration上的坑