學習|Android屬性動畫的組合動畫之一–AnimationSet

  • 2019 年 11 月 25 日
  • 筆記

學更好的別人,

做更好的自己。

——《微卡智享》

本文長度為2739,預計閱讀7分鐘

Android組合動畫

上篇文章我們介紹了《學習|Android屬性動畫的基礎介紹》,裡面只做了一個簡單的動畫例子,其實真正使用的時候一般我們都是通過動畫的多樣組合進行播放的,本章就專門介紹一下Android中的動畫組合之一AnimationSet的介紹。

動畫組合的四種方式

AnimationSet和Animatorset

一般來說,Android實現動畫組合有四種方式,分別是:

  • AnimationSet
  • AnimatorSet
  • PropertyValuesHolder
  • ViewPropertyAnimator

個人來說我用到的基本就前兩種,所以也主要就介紹一下前兩種。前兩種的名稱差不多,其實也是有其不同之處,這裡簡單介紹一下兩個的不同點。

AnimationSet和Animatorset

數字1代表AnimationSet 數據2代表Animatorset

1

動畫都是是同時執行

不支援背景色的修改

Animation 的子類

2

動畫可以按先後順序執行

支援背景色的修改

Animator 的子類

AnimationSet的使用

微卡智享

一般來我說我們在App中常用的都是一些視圖動畫:包括透明動畫(AlphaAnimation)旋轉動畫(RotateAnimation)移動動畫(TranslateAnimation)縮放動畫(ScaleAnimation),我們用AnimationSet就可以把這些動畫組合起來實現我們想要的效果。

我們通過定義上面那四種動畫組合,再使用animationSet.addAnimation的函數把動畫加進來即可實現該效果。下面是列出來AnimationSet的幾個比較常用的函數方法:

函數名

參數

說明

構造函數

boolean interpolator

true:代表使用默認的interpolatorfalse:代表使用自定義interpolator

addAnimation

Animation a

添加定義好的動畫效果

setDuration

long time

設置播放時長(毫秒)

setRepeatCount

int count

設置重放次數

setRepeatMode

int repeatmode

設置重放模式

setInterpolator

interpolator i

設置插值器

setFillAfter

boolean b

是否保持動畫完成後的位置 ‍

setFillBefore

boolean b

是否保持動畫開始時的狀態 ‍

cancel

取消AnimationSet

reset

釋放AnimationSet

劃重點

上面的函數中setRepeatCount和setRepeatMode兩個函數我用別的顏色標註了,主要是因為在使用的過程中發現不起作用,後來在網上找了找資料後發現在AnimationSet使用這個沒有效果,需要在對應的Animaion的動畫裡面設置才行。

程式碼實現

微卡智享

我們在還是用上一章中那個Demo,新建一個AnimationScale的函數

private void AnimationScale() {          //構造方法的入參如果是「true」,則代表使用默認的interpolator,如果是「false」則代表使用自定義interpolator          AnimationSet animationSet=new AnimationSet(false);            //透明度從0至1          AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);          //旋轉兩圈          RotateAnimation rotateAnimation=new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);          //放大十倍          ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 10, 1f, 10, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);          scaleAnimation.setRepeatCount(1);          scaleAnimation.setRepeatMode(Animation.REVERSE);          //平移距離x方向為父控制項寬的30.5%,y方向為父控制項高的30.5%          TranslateAnimation translateAnimation=new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_PARENT, 0.305f, Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_PARENT, 0.305f);          translateAnimation.setRepeatCount(1);          translateAnimation.setRepeatMode(Animation.REVERSE);          animationSet.addAnimation(alphaAnimation);          animationSet.addAnimation(rotateAnimation);          animationSet.addAnimation(scaleAnimation);          animationSet.addAnimation(translateAnimation);          //設置動畫時長為1秒          animationSet.setDuration(2000);          animationSet.setRepeatMode(AnimationSet.REVERSE);          //設置插值器為先加速再減速          animationSet.setInterpolator(new AccelerateDecelerateInterpolator());          //動畫完成後保持位置          animationSet.setFillAfter(false);          //保持動畫開始時的狀態          animationSet.setFillBefore(true);          //取消動畫          animationSet.cancel();          //釋放資源          animationSet.reset();          //開始動畫          tvshow.startAnimation(animationSet);        }

然後在btntest2的按鈕下加入點擊事件調用剛才創建的函數

運行起來的效果如圖