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

Android 補間動畫(Animation)完全整理:XML 與程式碼雙路實作

編輯:關於 Android 程式設計

Android 最基礎的動畫方案由 Animation 類別提供,兩種撰寫方式都支援:一是寫在 XML 裡,二是用 Java 程式碼動態產生。這篇把四種基本動畫與插入器的細節一次講清楚。

一、set 標籤的共用屬性

<set> 是所有動畫的根節點,底下可以掛多組子動畫:

表一:SET 屬性
屬性說明備註
android:shareInterpolator是否共用插入器共用時四個子節點使用同一個插入器
android:interpolator指定動畫插入器透過系統資源引用
android:fillEnabled填充總開關設為 true 時 fillAfter 與 fillBefore 都會生效,此時會忽略個別設定
android:fillAfter動畫結束後是否保留轉換結果boolean
android:fillBefore動畫開始前是否套用轉換boolean
android:repeatMode重複模式"restart" 從頭開始;"reverse" 從末尾反推
android:repeatCount重複次數integer,-1 為無限循環
android:duration動畫持續時間integer
android:startOffset動畫間隔(執行前停留時間)long
android:zAdjustment定義動畫 z 軸順序的變化[normal] / [top] / [bottom]
android:detachWallpaper—boolean

二、插入器(Interpolator):動畫的速度靈魂

插入器決定了動畫的「速度曲線」,也就是我們對動畫手感最直接的感受來源。

  1. @android:anim/accelerate_decelerate_interpolator:先加速後減速。
  2. @android:anim/accelerate_interpolator:一路加速。
  3. @android:anim/decelerate_interpolator:一路減速。
  4. @android:anim/anticipate_interpolator:先往反方向退一小步,再沿設定方向前進。
  5. @android:anim/anticipate_overshoot_interpolator:先反退,衝過終點一點點,再回彈到最終位置。
  6. @android:anim/bounce_interpolator:抵達終點時連續彈跳幾下後停止。
  7. @android:anim/cycle_interpolator:週期性循環運動。
  8. @android:anim/linear_interpolator:等速移動。
  9. @android:anim/overshoot_interpolator:快速衝到終點並超出,再折回。

以 anticipate 系列為例,軌跡大致如下:

Interpolator 物件資源 ID功能作用
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator先加速再減速
AccelerateInterpolator@android:anim/accelerate_interpolator加速
AnticipateInterpolator@android:anim/anticipate_interpolator先回退一小步然後加速前進
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator在上一個基礎上超出終點一小步再回到終點
BounceInterpolator@android:anim/bounce_interpolator最後階段彈球效果
CycleInterpolator@android:anim/cycle_interpolator週期運動
DecelerateInterpolator@android:anim/decelerate_interpolator減速
LinearInterpolator@android:anim/linear_interpolator等速
OvershootInterpolator@android:anim/overshoot_interpolator快速到達終點並超出一小步,最後回到終點

三、四種基本動畫的屬性表

表二:alpha(透明度)

屬性說明備註
android:fromAlpha動畫開始時的透明度float [0,1],0 全透明、1 全不透明
android:toAlpha動畫結束時的透明度float [0,1]
android:duration動畫持續時間integer

表三:scale(縮放)

屬性說明備註
android:fromXScale / toXScaleX 軸的伸縮尺寸起訖值0.0 表示收縮到消失、1.0 正常,小於 1 縮小、大於 1 放大
android:fromYScale / toYScaleY 軸的伸縮尺寸起訖值同上
android:pivotX縮放相對於物件的 X 基準位置[0%,100%],50% 為正中央
android:pivotY縮放相對於物件的 Y 基準位置[0%,100%],50% 為正中央

表四:translate(位移)

屬性說明備註
android:fromXDelta / toXDeltaX 軸位置的起訖值預設以自己為參照物
android:fromYDelta / toYDeltaY 軸位置的起訖值預設以自己為參照物

表五:rotate(旋轉)

屬性說明備註
android:fromDegrees起始角度負數逆時針、正數順時針
android:toDegrees結束角度可大於 360 度
android:pivotX / pivotY旋轉中心點[0%,100%],50% 為正中央

四、alpha 實作

XML 寫法:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
     android:fillAfter="true"
     android:fillBefore="false" >
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
</set>

這裡有個常被忽略的細節:如果你希望動畫停在最後一幀,android:fillAfter="true" 與 fillBefore="false" 必須寫在 <set> 上才有效,寫在子節點是不會生效的。

程式碼寫法:

Animation anim = new AlphaAnimation(0f, 1.0f);
anim.setDuration(2000);
anim.setRepeatCount(Animation.INFINITE);
anim.setRepeatMode(Animation.RESTART);

五、scale 實作

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
     android:fillAfter="true"
     android:interpolator="@android:anim/decelerate_interpolator" >
    <scale android:duration="2000"
           android:fromXScale="0.5"
           android:fromYScale="0.5"
           android:pivotX="50.0%"
           android:pivotY="50.0%"
           android:repeatCount="3"
           android:repeatMode="reverse"
           android:toXScale="1.0"
           android:toYScale="1.0" />
</set>

pivotX / pivotY 設為 50% 代表從容器正中央開始縮放,這是實務上最常用的一組值。

Animation anim1 = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, 0.5f, 0.5f);
anim1.setDuration(2000);
anim1.setRepeatCount(3);
anim1.setRepeatMode(Animation.REVERSE);
anim1.setInterpolator(this, interpolator.accelerate_decelerate);
anim1.setFillAfter(true);

六、translate 實作

Animation animTranlate = new TranslateAnimation(0, 0, 0, 400);
animTranlate.setDuration(2000);
animTranlate.setFillAfter(true);
animTranlate.setInterpolator(this, interpolator.bounce);
text.startAnimation(animTranlate);

七、rotate 實作

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
     android:fillAfter="true"
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
    <rotate android:duration="2000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="180" />
</set>
Animation animRotate = new RotateAnimation(0f, 360f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
animRotate.setDuration(2000);
animRotate.setInterpolator(this, interpolator.bounce);
animRotate.setFillAfter(true);
animRotate.setStartOffset(2000);   // 執行前停留時間
text.startAnimation(animRotate);

八、組合動畫

XML 版本把多組子動畫塞進同一個 <set> 即可:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
     android:duration="2000"
     android:fillAfter="true"
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" >
    <scale android:fromXScale="0"
           android:fromYScale="0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:toXScale="1.0"
           android:toYScale="1.0" />
</set>

程式碼版本用 AnimationSet 收集:

Animation anim1 = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, 0.5f, 0.5f);
anim1.setDuration(2000);
anim1.setRepeatCount(3);
anim1.setRepeatMode(Animation.REVERSE);
anim1.setInterpolator(this, interpolator.accelerate_decelerate);
anim1.setFillAfter(true);

Animation animTranlate = new TranslateAnimation(0, 0, 0, 400);
animTranlate.setDuration(2000);
animTranlate.setFillAfter(true);
animTranlate.setInterpolator(this, interpolator.bounce);

Animation animRotate = new RotateAnimation(0f, 360f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);
animRotate.setDuration(2000);
animRotate.setInterpolator(this, interpolator.bounce);
animRotate.setFillAfter(true);

AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(animRotate);
animationSet.addAnimation(animTranlate);
animationSet.addAnimation(anim1);
text.startAnimation(animationSet);

構造子裡那個 true 參數等同於 XML 的 shareInterpolator,代表子動畫共用同一個插入器;若各子動畫想跑各自的速度曲線,記得改傳 false。

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