6.聯合動畫:代碼和XML實現

  • Animator:
    • AnimatorSet
    • ValueAnimator
      • ObjectAnimator
      • TimerAnimator

其他方法實現AnimatorSet:

  • PropertyValuesHolder。
  • view自帶方法。
ivSplashBg.animate().scaleX(1.2f).scaleY(1.2f).setDuration(2000)
                .setStartDelay(400)
                .start();

AnimatorSet:

AnimatorSet針對ValueAnimator和ObjectAnimator都是適用的,但一般而言,我們不會用到ValueAnimator的組合動畫,所以我們這篇僅講解ObjectAnimator下的組合動畫實現。

playTogether/playSequentially

// 按次序播放動畫
public void playSequentially(Animator... items); 
public void playSequentially(List<Animator> items);

// 一起播放動畫
public void playTogether(Animator... items);
public void playTogether(Collection<Animator> items);
ObjectAnimator anim1 = ObjectAnimator.ofInt(mTv1, "BackgroundColor",  0xffff00ff, 0xffffff00, 0xffff00ff);

ObjectAnimator anim2 = ObjectAnimator.ofFloat(mTv1, "translationY", 0, 400, 0);
anima2.setStartDelay(2000);
anima2.setRepeatCount(ValueAnimator.INFINITE);

ObjectAnimator anim3 = ObjectAnimator.ofFloat(mTv2, "translationY", 0, 400, 0);
anim3.setStartDelay(2000);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(anim1, anim2, anim3);
animatorSet.setDuration(2000);
animatorSet.start();

無論是playTogether還是playSequentially方法,它們只是,僅僅是激活了動畫什么時候開始,并不參與動畫的具體操作。
例如:如果是playTogether,它只負責這個歌動畫什么時候一起激活,至于anim1/anim2/anim3...哪個馬上開始,哪個有延遲,哪個會無限重復,set都不管,只負責一起激活。
如果是playSequentially,它只負責什么時候開始激活第一個(因為有可能set設置延遲),并在第一個動畫結束的時候,激活第二個,以此類推。

image

通用的一些方法單獨設置和AnimatorSet設置的區別:

無論是ObjectAnimator還是AnimatorSet都有這些方法,因為兩者都是繼承的Animator類的方法,那么當ObjectAnimator和AnimatorSet中都有設置同意函數時,以哪個為準?

  • 以set為準:
//設置單次動畫時長
public AnimatorSet setDuration(long duration);
//設置加速器
public void setInterpolator(TimeInterpolator interpolator)
//設置ObjectAnimator動畫目標控件
public void setTarget(Object target)
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mTv1, "translationY", 0, 400, 0);
anim1.setDuration(500000000);

ObjectAnimator anim2 = ObjectAnimator.ofFloat(mTv2, "translationY", 0, 400, 0);
anim2.setDuration(3000);
anim2.setRepeatCount(3);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(tv2TranslateY).with(tv1TranslateY);
animatorSet.setDuration(2000);
animatorSet.start();

setDuration()是指單個動畫的時間,并不是指總共做完這個動畫過程的時間
比如:anim2中設置了3000ms,重復3次。是指每次3000ms,不是3次3000ms。
另外:animatorSet設置了時間以后,anim1/anim2雖然也設置了,但是這時以set為準。即,anim1/anim2的單個動畫時間為2000ms。只不過anim2是每次2000ms,重復3次,共6000ms。

  • 不以set為準:setStartDelay
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mTv1, "translationY", 0, 400, 0);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mTv2, "translationY", 0, 400, 0);
anim2.setStartDelay(2000);

AnimatorSet animatorSet = new AnimatorSet();
animatorSet.addListener(new Animator.AnimatorListener(){...});
animatorSet.play(anim1).with(anim2);
animatorSet.setStartDelay(3000);
animatorSet.setDuration(2000);
animatorSet.start();

setStartDelay不會覆蓋單個動畫的該方法,只會延長set的激活時間。所以,上面代碼中動畫的啟動過程是:3000ms后set開始激活動畫,anim1啟動,anim2再過2000ms啟動。

注意play(anim1).with(anim2)和play(anim2).with(anim1)的區別:

  • play(anim1).with(anim2):3000ms后set開始激活動畫,anim1啟動,anim2再過2000ms啟動。
  • play(anim2).with(anim1):3000ms后set開始激活動畫,再過2000ms后啟動anim2,并且啟動anim1.

set監聽:addListener監聽的是AnimatorSet的start/end/cacle/repeat。不會監聽anim1/anim2的動畫狀態的。

AnimatorSet.Builder函數

//調用AnimatorSet中的play方法是獲取AnimatorSet.Builder對象的唯一途徑
//表示要播放哪個動畫
public Builder play(Animator anim);

//和前面動畫一起執行
public Builder with(Animator anim)
//執行前面的動畫后才執行該動畫
public Builder before(Animator anim)
//執行先執行這個動畫再執行前面動畫
public Builder after(Animator anim)
//延遲n毫秒之后執行動畫
public Builder after(long delay)

play的對象為基準,無論是after還是before。


XML實現

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容