Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 屬性動畫(Property Animation)完全指南

編輯:關於 Android 程式設計

一、概述

Android 提供了幾種動畫類型:View Animation、Drawable Animation 與 Property Animation。View Animation 相當簡單,但只能支援縮放、平移、旋轉、透明度等基本動畫,且有明顯局限。舉例來說:若你希望 View 有顏色切換動畫、想使用 3D 旋轉動畫,或是希望動畫結束後 View 就停在當前位置——這些都是 View Animation 做不到的。這正是 Property Animation 誕生的原因。本篇將詳細介紹 Property Animation 的用法;至於 Drawable Animation,這裡就略過了。

二、相關 API

顧名思義,Property Animation 就是透過動畫的方式改變對象的屬性。我們先來了解幾個關鍵屬性:

Duration:動畫持續時間,預設 300ms。

Time interpolation:時間差值,也就是 LinearInterpolator、AccelerateDecelerateInterpolator 這類物件,用來定義動畫的變化率。

Repeat count and behavior:重複次數與重複模式,可定義重複幾次、重複時是從頭開始還是反向播放。

Animator sets:動畫集合,可定義一組動畫,一起執行或順序執行。

Frame refresh delay:幀刷新延遲,即動畫多久刷新一次幀,預設 10ms,但最終仍取決於系統當前狀態,基本不用理會。

相關類別如下:

ObjectAnimator:動畫的執行類,後文詳細介紹。
ValueAnimator:動畫的執行類,後文詳細介紹。
AnimatorSet:用於控制一組動畫的執行——線性、一起、各動畫的先後順序等。
AnimatorInflater:用於載入屬性動畫的 xml 檔。
TypeEvaluator:類型估值,主要用於設定動畫操作屬性的值。
TimeInterpolator:時間插值,前面已介紹。

總的來說,屬性動畫就是由動畫執行類設定動畫操作對象的屬性、持續時間、起始與結束屬性值、時間差值等參數,然後由系統依這些參數動態地改變對象的屬性。

三、以 ObjectAnimator 實作動畫

之所以把 ObjectAnimator 放在第一個介紹,是因為它實作起來最簡單——一行程式碼,瞬間完成動畫。先看個例子,版面配置如下:

 android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/id_container">  

很簡單,就一張妹子圖片。Activity 程式碼:

 public class ObjectAnimActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xml_for_anim); } public void rotateyAnimRun(View view) { ObjectAnimator .ofFloat(view, "rotationX", 0.0F, 360.0F) .setDuration(500) .start(); } }

看了上面的例子,因為操作的屬性只有一個。那如果我希望一個動畫能同時讓 View 縮小並淡出(涉及 scaleX、scaleY、alpha 三個屬性),只用 ObjectAnimator 該怎麼辦?可以這樣做:

 public void rotateyAnimRun(final View view) { ObjectAnimator anim = ObjectAnimator .ofFloat(view, "zhy", 1.0F, 0.0F) .setDuration(500); anim.start(); anim.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float cVal = (Float) animation.getAnimatedValue(); view.setAlpha(cVal); view.setScaleX(cVal); view.setScaleY(cVal); } }); }

做法是:把設定屬性時傳入的字串隨便寫一個該對象沒有的屬性(此處不管它),我們只需要它依時間插值與持續時間算出的那個值,再自行手動呼叫 setter 去更新各項屬性即可。

這個例子想說明的是:有時候換個思路、別被 API 束縛,善用部分 API 提供的功能,也能實現好玩的效果。比如你想實作拋物線效果(水平方向 100px/s、垂直方向加速度 200px/s²),就可以自己用 ObjectAnimator 試試。

其實還有更簡單的方式能讓一個動畫同時更改多個效果:使用 PropertyValuesHolder。

 public void propertyValuesHolder(View view) { PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f); PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f); PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f); ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY, pvhZ).setDuration(1000).start(); }
四、以 ValueAnimator 實作動畫

ValueAnimator 的用法與 ObjectAnimator 很類似。先看看讓 View 垂直移動的動畫程式碼:

 public void verticalRun(View view) { ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight - mBlueBall.getHeight()); animator.setTarget(mBlueBall); animator.setDuration(1000).start(); }

乍看之下似乎和 ObjectAnimator 沒兩樣?但仔細看會發現,這裡並沒有設定要操作的屬性,也就是說上述程式碼其實不會有任何效果——因為沒有指定屬性。版面配置如下:

 android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/id_container">   

左上角一個小球,底部兩個按鈕。先看一個自由落體的程式碼:

 /** *自由落體 *@paramview */ public void verticalRun(View view) { ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight - mBlueBall.getHeight()); animator.setTarget(mBlueBall); animator.setDuration(1000).start(); //animator.setInterpolator(value) animator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mBlueBall.setTranslationY((Float) animation.getAnimatedValue()); } }); }

與 ObjectAnimator 不同的地方在於,這裡由我們自己設定元素屬性的更新。雖然多了幾行程式碼,但靈活性似乎更高了。

再來一個例子:若希望小球做拋物線運動(水平方向 100px/s、垂直方向加速度 200px/s²),分析可知它只與時間有關,但隨時間變化,橫向與縱向的移動速率並不相同,該怎麼實作呢?此時就要重寫 TypeEvaluator 了,因為我們在時間變化的同時,需要回傳給對象兩個值——x 當前位置與 y 當前位置:

 /** *拋物線 *@paramview */ public void paowuxian(View view) { ValueAnimator valueAnimator = new ValueAnimator(); valueAnimator.setDuration(3000); valueAnimator.setObjectValues(new PointF(0, 0)); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.setEvaluator(new TypeEvaluator() { //fraction=t/duration @Override public PointF evaluate(float fraction, PointF startValue, PointF endValue) { Log.e(TAG, fraction * 3 + ""); //x方向200px/s,則y方向0.5*10*t PointF point = new PointF(); point.x = 200 * fraction * 3; point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3); return point; } }); valueAnimator.start(); valueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { PointF point = (PointF) animation.getAnimatedValue(); mBlueBall.setX(point.x); mBlueBall.setY(point.y); } }); }

可以看到,由於 ofInt、ofFloat 等無法使用,我們自訂了一個 TypeEvaluator,每次依當前時間回傳一個 PointF 物件(PointF 與 Point 的差別在於 x、y 的單位一個是 float、一個是 int;RectF 與 Rect 也是),PointF 中包含了 x、y 的當前位置,然後我們在監聽器中取出並動態設定屬性。

是不是有種「兩個鐵球同時落地」的感覺?自訂 TypeEvaluator 傳入的泛型可依自身需求設計成自訂的 Bean。

至此,我們已分別講解了 ValueAnimator 與 ObjectAnimator 的動畫實作、兩者差異、如何善用部分 API 自行更新屬性,以及自訂 TypeEvaluator 滿足需求。至於插值(Interpolator)的設計,我認為預設那一系列實作類已經很夠用,很少有人會自己去設計超級變態的插值,這裡便略過。

五、監聽動畫事件

動畫通常只是輔助效果。比如我要刪除一個元素,可能希望有個淡出效果,但最終還是要真的把它刪掉,不能透明度歸零卻仍佔著位置,所以我們需要知道動畫何時結束。為此可以加入動畫監聽:

 public void fadeOut(View view) { ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f); anim.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { Log.e(TAG, "onAnimationStart"); } @Override public void onAnimationRepeat(Animator animation) { Log.e(TAG, "onAnimationRepeat"); } @Override public void onAnimationEnd(Animator animation) { Log.e(TAG, "onAnimationEnd"); ViewGroup parent = (ViewGroup) mBlueBall.getParent(); if (parent != null) parent.removeView(mBlueBall); } @Override public void onAnimationCancel(Animator animation) { Log.e(TAG, "onAnimationCancel"); } }); anim.start(); }

如此一來便能監聽動畫的開始、結束、取消、重複等事件。但有時候我只想知道結束,這麼長的程式碼實在難以接受,那你可以改用 AnimatorListenerAdapter:

 anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { Log.e(TAG, "onAnimationEnd"); ViewGroup parent = (ViewGroup) mBlueBall.getParent(); if (parent != null) parent.removeView(mBlueBall); } });

最後再來玩玩多動畫的组合。版面配置:

 android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal"> 

Activity 程式碼:

 public class AnimatorSetActivity extends Activity { private ImageView mBlueBall; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.anim_set); mBlueBall = (ImageView) findViewById(R.id.id_ball); } public void togetherRun(View view) { ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX", 1.0f, 2f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY", 1.0f, 2f); AnimatorSet animSet = new AnimatorSet(); animSet.setDuration(2000); animSet.setInterpolator(new LinearInterpolator()); //兩個動畫同時執行 animSet.playTogether(anim1, anim2); animSet.start(); } public void playWithAfter(View view) { float cx = mBlueBall.getX(); ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX", 1.0f, 2f); ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY", 1.0f, 2f); ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall, "x", cx, 0f); ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall, "x", cx); /** *anim1,anim2,anim3同時執行 *anim4接著執行 */ AnimatorSet animSet = new AnimatorSet(); animSet.play(anim1).with(anim2); animSet.play(anim2).with(anim3); animSet.play(anim4).after(anim3); animSet.setDuration(1000); animSet.start(); } }

這裡實作了兩個效果:一個是多動畫同時執行,另一個是多動畫按次序執行。你可能會說「這一看就是一堆動畫塞在一起執行,用 AnimatorSet 就好了」——沒錯,AnimatorSet 正是為此而生;不過若硬要用單一 ObjectAnimator 也能透過前面介紹的 PropertyValuesHolder 等技巧來達成類似效果,端看需求取捨。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved