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

Android 動畫總結:View Animation 與 Property Animation

編輯:關於 Android 程式設計

Android 主要提供兩類動畫機制:

1. Property Animation(屬性動畫)——相關類別位於 android.animation 套件。

2. View Animation(視圖動畫)——相關類別位於 android.view.animation 套件,顧名思義它是針對「視圖」的動畫。

View Animation 的兩種型別

(1) Frame Animation(影格動畫)

對應的類別是 AnimationDrawable。你可以把它單純理解為一個 Drawable,使用方式與其他 Drawable 資源大同小異,一樣可以在 XML 或程式碼中實作:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();

啟動動畫是呼叫 start(),但要注意這個方法不能寫在 onCreate() 裡,因為此時動畫資源尚未完全載入到視窗;比較恰當的位置是放在 onWindowFocusChanged() 中。

(2) Tween Animation(補間動畫)

補間動畫是對一張圖片進行的變換效果,例如縮放。查看原始碼不難理解:以 ScaleAnimation 為例,縮放效果是透過 Matrix 矩陣變換來實現,本質上就是動態地對位圖做變換操作。這也說明了為何補間動畫只有四種——AlphaAnimation、ScaleAnimation、TranslateAnimation 與 RotateAnimation,因為一張圖片的矩陣變換也就這四類。

Animation 抽象類是這四個類別的基底,我們也可以模仿它們,透過矩陣變換或其他操作位圖的方式實作自訂補間動畫,例如讓其中兩種效果同時變化。

補間動畫的 XML 基本格式

這類動畫通常作為獨立的動畫資源檔,存放在 res/anim/ 資料夾下,資料夾中每個資源對應的都是 Animation 類別的物件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator=["true" | "false"] >
    <alpha android:fromAlpha="float" android:toAlpha="float" />
    <scale android:fromXScale="float" android:toXScale="float"
        android:fromYScale="float" android:toYScale="float"
        android:pivotX="float" android:pivotY="float" />
    <translate android:fromXDelta="float" android:toXDelta="float"
        android:fromYDelta="float" android:toYDelta="float" />
    <rotate android:fromDegrees="float" android:toDegrees="float"
        android:pivotX="float" android:pivotY="float" />
    <set> ... </set>
</set>

其中唯一稍有難度的是 interpolator 屬性,會在後續文章中再詳細說明。在程式碼中,可用 AnimationUtils.loadAnimation() 載入動畫資源,再呼叫 startAnimation() 啟動:

ImageView image = (ImageView) findViewById(R.id.image);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
image.startAnimation(hyperspaceJump);

Property Animation(屬性動畫)

兩者最大的差異在於:View Animation 的作用對象是 View,而 Property Animation 的作用對象則是「所有物件」。屬性動畫的基底類別是 Animator,它在 XML 中同樣是一種動畫資源,只是檔案要放在 res/animator/ 資料夾中:

<set android:ordering="sequentially">
    <set>
        <objectAnimator android:propertyName="x" android:duration="500" android:valueTo="400" android:valueType="intType"/>
        <objectAnimator android:propertyName="y" android:duration="500" android:valueTo="300" android:valueType="intType"/>
    </set>
    <objectAnimator android:propertyName="alpha" android:duration="500" android:valueTo="1f"/>
</set>

啟動方式如下:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator);
set.setTarget(myObject);
set.start();

總結來說:視圖動畫針對 View,又可細分為「影格動畫」(可視為完整的 Drawable 資源)與「補間動畫」(透過矩陣變換操作單一位圖);屬性動畫則針對所有 Object,能實現更靈活的效果。

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