原文地址 http://blog.csdn.net/qq_25806863/article/details/70226784
AnimatorSet是對屬性動畫的一個集合,可以讓很多動畫按一定順序或者 同時進行。
概覽
先看一下屬性動畫的結構,View動畫的基類是Animation,屬性動畫的基類是Animator
- ValueAnimator:這個動畫是針對屬性的值進行動畫的 ,不會對UI造成改變,不能直接實現動畫效果。需要通過對動畫的監聽去做一些操作,在監聽中將這個值設置給對應的屬性,對應的屬性纔會改變。
- ObjectAnimator:直接動畫所給的對象,他會調用對象對應屬性的get/set方法吧屬性的值設置給對象的屬性,直接實現動畫效果。
簡單使用
AnimatorSet主要就是方便整合一大堆動畫的,這裏先定義兩個動畫
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnShow,"translationX",0,300);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btnShow,"alpha",0.1f,1f);
然後使用AnimatorSet整合起來:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(objectAnimator,objectAnimator1);
animatorSet.setDuration(2000);
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.start();
裏面其他方法跟一般的動畫沒什麼區別,Duration可以單獨設置也可以一起設置,都設置以AnimatorSet的爲準。同樣,animatorSet.setTarget也是以AnimatorSet的爲準。如果加上一句animatorSet.setTarget(btnStart);
那麼動畫的就是btnStart了,而不是btnShow。
主要是看playSequentially方法,這個方法參數就是要執行的屬性動畫 ,playSequentially表示按順序執行這些動畫:
如果調用animatorSet.playTogether(objectAnimator,objectAnimator1);
表示這些動畫一起執行:
AnimatorSet.Builder
AnimatorSet有一個內部類AnimatorSet.Builder,AnimatorSet只提供了playSequentially和playTogether兩種方式,而AnimatorSet.Builder可以自由組合各種方式。
AnimatorSet.Builder有四個方法:
方法 | 解釋 |
---|---|
after(long delay) | 在一定時間後執行play裏的動畫 |
after(Animator anim) | 先執行after裏的動畫,再執行play裏的動畫 |
before(Animator anim) | 先執行play裏的動畫,再執行after裏的動畫 |
with(Animator anim) | 一起執行 |
AnimatorSet.Builder通過調用AnimatorSet的play方法獲取。
下面定義兩個動畫,用after:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btnShow, "translationX", 0, 300);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(btnShow, "rotation", 0, 360);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(2000);
animatorSet.play(objectAnimator).after(objectAnimator1);
animatorSet.start();
before:
animatorSet.play(objectAnimator).before(objectAnimator1);
with:
animatorSet.play(objectAnimator).with(objectAnimator1);
對AnimatorSet的監聽
AnimatorSet內部有一個私有的監聽AnimatorSetListener,這個用來他自己處理內部動畫。
自己加監聽一般是Animator.AnimatorListener
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.i(TAG, "onAnimationStart: ");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.i(TAG, "onAnimationEnd: ");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.i(TAG, "onAnimationCancel: ");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.i(TAG, "onAnimationRepeat: ");
}
});
這個時候,AnimatorSet中的所有動畫成了一個整體,onAnimationEnd是在所有動畫完成後纔會調用,不管是順序執行還是同時執行。
XML表示
AnimatorSet在XML中的標籤也是<set>
,但是也是要把文件定義在res/animator中
如 res/animator/anim_set:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:valueFrom="0"
android:valueTo="360"
android:propertyName="rotation"/>
<objectAnimator
android:valueFrom="0"
android:valueTo="360"
android:propertyName="translationX"/>
</set>
java中:
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(context,R.animator.anim_set);
animatorSet.setTarget(btnShow);
animatorSet.setDuration(2000);
animatorSet.start();
android:orderin
android:orderin有兩個值,sequentially和together,分別對應playSequentially和playTogether兩個方法的效果。