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

Android Tween 補間動畫完全解析(下):用程式碼複刻 XML 效果

編輯:關於 Android 程式設計

上一篇我們把 Tween 動畫的 XML 屬性配置與 Interpolator 插值器講透了,也推薦先看完上篇再回到這裡。本篇換一個視角:完全用 Java 程式碼,把上篇那些 XML 標籤一個一個「翻譯」成物件和方法呼叫。讀完之後,你應該能做到看到一段動畫 XML,腦中直接浮現出對應的建構方式。

一、先回顧:四種動畫與它們的類

Tween 動畫一共四兄弟:alpha(透明度漸變)、scale(縮放)、translate(位移)、rotate(旋轉)。它們都繼承自 Animation,因此共用一批公共方法;同時各自又有一組獨屬屬性。XML 裡寫的每一個屬性,在程式碼裡幾乎都能找到一一對應的 setter。

XML 根標籤對應類別動畫類型
<alpha>AlphaAnimation透明度漸變
<scale>ScaleAnimation縮放漸變
<translate>TranslateAnimation位移漸變
<rotate>RotateAnimation旋轉漸變

再看從 Animation 繼承下來的公共屬性:

XML 屬性對應方法說明
android:durationsetDuration(long)動畫時長(毫秒)
android:fillEnabledsetFillEnabled(boolean)需與 fillBefore 搭配使用
android:fillBeforesetFillBefore(boolean)動畫結束後是否停留在起始狀態
android:fillAftersetFillAfter(boolean)動畫結束後是否停留在結束狀態
android:repeatModesetRepeatMode(int)重複模式
android:repeatCountsetRepeatCount(int)重複次數
android:interpolatorsetInterpolator(Interpolator)插值器

這些屬性上篇已經逐一解釋過,這裡不再重複。接著直接進主題。

二、AlphaAnimation

alpha 動畫只有兩個獨屬屬性:android:fromAlpha 與 android:toAlpha,取值範圍 0.0~1.0,0.0 全透明、1.0 全不透明。

對應到程式碼,只要把這兩個值放進建構子即可。AlphaAnimation 有兩個建構子,第一個(接收 Resources 與 AttributeSet,供 XML 解析使用)一般不用,我們關注的是接收兩個 float 的那一個:

AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
alphaAnimation.setDuration(3000);
alphaAnimation.setRepeatCount(2);
alphaAnimation.setFillAfter(true);
ivImage.startAnimation(alphaAnimation);

這段程式碼的效果,與上篇那段 fromAlpha=0、toAlpha=1 的 XML 完全等價:圖片從透明漸漸浮現,重複兩次,動畫結束後停在最終狀態。

三、ScaleAnimation

ScaleAnimation 的獨屬屬性如下:

屬性含義
android:fromXScale / toXScaleX 軸方向的起始/結束縮放比例(相對於自身)
android:fromYScale / toYScaleY 軸方向的起始/結束縮放比例(相對於自身)
android:pivotX / pivotY縮放基準點座標

它有四個建構子,最完整的一個長這樣:

ScaleAnimation scaleAnimation = new ScaleAnimation(
        fromX, toX, fromY, toY,
        pivotXType, pivotXValue, pivotYType, pivotYValue);

這裡最容易讓人困惑的就是 pivotXType / pivotXValue 這對參數(Y 軸同理)。它們其實就是 XML 裡 pivotX="50"、"50%"、"50%p" 三種寫法的程式化表達:

Type 取值對應 XML 寫法含義
Animation.ABSOLUTE50相對於 View 左上角(原點)偏移 50px
Animation.RELATIVE_TO_SELF50%左上角加上自身寬度的 50%
Animation.RELATIVE_TO_PARENT50%p左上角加上父容器寬度的 50%

也就是說,Type 負責告訴系統「這個數字該怎麼解讀」,Value 才是數字本身。上篇那段 5 秒、從 2 倍縮回 1 倍、以自身中心為基準的 XML,對應程式碼為:

ScaleAnimation scaleAnimation = new ScaleAnimation(
        2.0f, 1.0f, 2.0f, 1.0f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(5000);
ivImage.startAnimation(scaleAnimation);

四、TranslateAnimation

位移動畫的獨屬屬性是 fromXDelta、fromYDelta、toXDelta、toYDelta,起始點與終點的 X/Y 座標,同樣支援數值、百分比、百分比 p 三種寫法。

它有兩個常用建構子:一個只吃四個 float,此時預設使用 Animation.ABSOLUTE(等同 XML 中直接寫數值);另一個則把 Type 與 Value 分開傳入,控制力更強:

TranslateAnimation translateAnimation = new TranslateAnimation(
        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
        Animation.ABSOLUTE, 200, Animation.ABSOLUTE, 300);
translateAnimation.setDuration(5000);
ivImage.startAnimation(translateAnimation);

這段程式碼讓 View 從原點出發,向右下移動 (200, 300) 像素,歷時 5 秒。

五、RotateAnimation

旋轉動畫的屬性包括 fromDegrees、toDegrees、pivotX、pivotY。角度為 float,正值順時針、負值逆時針;pivot 則決定繞哪個點轉。

RotateAnimation 的建構子層層遞進:只傳兩個角度時,預設繞 View 左上角旋轉;加上 pivotX / pivotY 之後可以指定基準點(此時預設 ABSOLUTE);最完整版本再各自加上 Type 參數。

RotateAnimation rotateAnimation = new RotateAnimation(
        0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
ivImage.startAnimation(rotateAnimation);

注意一個實務上的小陷阱:建構子裡寫 3000,而 XML 中寫的是 10000,程式碼與 XML 是兩份獨立的配置,改一邊不會影響另一邊,別把兩者搞混。

六、加上 Interpolator

為動畫指定插值器非常簡單,呼叫 setInterpolator() 即可。以下示範讓一個小球落下後反彈:

TranslateAnimation translateAnimation = new TranslateAnimation(
        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 250);
translateAnimation.setInterpolator(new BounceInterpolator());
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(2000);
ivDot.startAnimation(translateAnimation);

七、AnimationSet:讓多種動畫協同

前面都是單一動畫。當同一個 View 需要同時播放多種效果時,就要請出 AnimationSet。最常用的建構子只接收一個 boolean 參數 shareInterpolator:為 true 時,集合內所有動畫統一使用 AnimationSet 自身的插值器;為 false 時,各子動畫保留自己的插值器設定。

ScaleAnimation scaleAnimation = new ScaleAnimation(
        0f, 1f, 0f, 1f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);

RotateAnimation rotateAnimation = new RotateAnimation(
        0, 720,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);

TranslateAnimation translateAnimation = new TranslateAnimation(
        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 85,
        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);
translateAnimation.setDuration(1000);
translateAnimation.setStartOffset(3000);

AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
alphaAnimation.setDuration(1000);
alphaAnimation.setStartOffset(4000);

AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);

ivImage.startAnimation(animationSet);

這段程式碼的時間軸值得細看:前 3 秒縮放與旋轉同步進行,第 3~4 秒向右位移,第 4~5 秒淡出。setStartOffset() 正是控制這種「接力」節奏的關鍵,也是 XML 中 android:startOffset 的程式化寫法。

結語

寫到這裡,Tween 動畫的程式碼路徑基本走完了。可以發現規律非常清晰:XML 根標籤 → 對應 Animation 子類;XML 屬性 → 對應 setter 或建構子參數;Type/Value 兩段式表達 → Type 常數搭配比例值。掌握這條對應關係,之後無論看到什麼樣的動畫 XML,都能快速改寫成程式碼版本。

實務上仍需留意兩點:一是 Tween 動畫只改變「繪製時的視覺效果」,並不改變 View 的真實位置與大小,因此點擊事件的命中區域仍留在原處;二是動畫結束後若未設定 fillAfter,View 會瞬間彈回原位。這兩點也是後續 Property Animation(屬性動畫)試圖解決的問題,下一篇我們就來談它。

有疑問歡迎留言,發現謬誤也歡迎指正。

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