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

Android 動畫三大體系:補間、幀動畫與屬性動畫的 XML 與程式碼寫法

編輯:關於 Android 程式設計

Android 的動畫大致可分為三類:補間動畫(Tween)、幀動畫(Frame Animation)與屬性動畫(Property Animation,建立在值動畫之上)。下面分別說明它們在 XML 與程式碼中的配置方式。

一、補間動畫

包含 translate、scale、alpha、rotate 四種,XML 結構大同小異。多種動畫若要合併播放,最外層包一個 <set> 即可。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:duration="2000"
        android:fillAfter="true"
        android:fromXDelta="0"
        android:toXDelta="200"
        android:interpolator="@android:anim/cycle_interpolator" />
</set>

其中 interpolator 用來控制動畫的節奏與收尾行為,例如 cycleInterpolator 可讓動畫來回振盪:

<cycleInterpolator
    android:cycles="7"
    xmlns:android="https://schemas.android.com/apk/res/android" />

這段表示來回振盪 7 次。除 cycle 之外,常見的還有 overshoot、anticipate、accelerateDecelerate 等,另外三種動畫的用法完全相同。

程式碼中的補間動畫

public void translate(View v) {
    // ta = new TranslateAnimation(10, 100, 20, 200);
    TranslateAnimation ta = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 2f,
            Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 1.5f);
    ta.setDuration(2000);
    ta.setRepeatCount(1);
    ta.setRepeatMode(Animation.REVERSE);
    iv.startAnimation(ta);
}

public void scale(View v) {
    ScaleAnimation sa = new ScaleAnimation(
            0.5f, 2f, 0.1f, 3f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    sa.setDuration(2000);
    sa.setRepeatCount(1);
    sa.setRepeatMode(Animation.REVERSE);
    sa.setFillAfter(true);      // 動畫結束後保留終態
    iv.startAnimation(sa);
}

public void alpha(View v) {
    AlphaAnimation aa = new AlphaAnimation(0f, 1f);
    aa.setDuration(2000);
    aa.setRepeatCount(1);
    iv.startAnimation(aa);
}

public void rotate(View v) {
    RotateAnimation ra = new RotateAnimation(
            0, 720,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    ra.setDuration(2000);
    ra.setRepeatCount(1);
    ra.setRepeatMode(Animation.REVERSE);
    iv.startAnimation(ra);
}

public void fly(View v) {
    AnimationSet set = new AnimationSet(false);
    set.addAnimation(ta);
    set.addAnimation(sa);
    set.addAnimation(ra);
    set.addAnimation(aa);
    iv.startAnimation(set);
}

要注意的是,AnimationSet(false) 中的 false 代表各子動畫不共用同一個插值器,若希望整體節奏一致可傳 true。

二、幀動畫

幀動畫把連續圖片依序播放,常用於開場動畫這類場合。在 animation-list 中以 item 描述每一幀:

<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/g1"  android:duration="200" />
    <item android:drawable="@drawable/g2"  android:duration="200" />
    <item android:drawable="@drawable/g3"  android:duration="200" />
    <item android:drawable="@drawable/g4"  android:duration="200" />
    <item android:drawable="@drawable/g5"  android:duration="200" />
    <item android:drawable="@drawable/g6"  android:duration="300" />
    <item android:drawable="@drawable/g7"  android:duration="300" />
    <item android:drawable="@drawable/g8"  android:duration="300" />
    <item android:drawable="@drawable/g9"  android:duration="200" />
    <item android:drawable="@drawable/g10" android:duration="200" />
    <item android:drawable="@drawable/g11" android:duration="200" />
</animation-list>

oneshot 決定動畫是否只播放一次,duration 則是該幀的持續時間(毫秒)。程式碼中的啟動方式有個關鍵細節——必須設為背景,才能轉型成 AnimationDrawable:

// 把幀動畫資源設為背景
iv.setBackgroundResource(R.drawable.frameanimation);
// 取得背景並啟動
AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
ad.start();

三、屬性動畫

屬性動畫與補間動畫最大的差別在於:它是真的去改變物件的屬性值,動畫結束後 View 的位置確實變了,而不只是畫面上「看起來」移動過。

public void translate(View v) {
    ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10f, 70f, 20f, 100f);
    oa.setDuration(2000);
    oa.setRepeatCount(1);
    oa.setRepeatMode(ValueAnimator.REVERSE);
    oa.start();
}

public void scale(View v) {
    ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 1.6f, 1.2f, 2f);
    oa.setDuration(2000);
    oa.start();
}

public void alpha(View v) {
    ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0f, 0.6f, 0.2f, 1f);
    oa.setDuration(2000);
    oa.start();
}

public void rotate(View v) {
    ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationY", 0f, 180f, 90f, 360f);
    oa.setDuration(2000);
    oa.setRepeatCount(1);
    oa.setRepeatMode(ValueAnimator.REVERSE);
    oa.start();
}

public void fly(View v) {
    AnimatorSet set = new AnimatorSet();

    ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10f, 70f, 20f, 100f);
    oa1.setDuration(2000);
    oa1.setRepeatCount(1);
    oa1.setRepeatMode(ValueAnimator.REVERSE);

    ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY", 10f, 70f, 20f, 100f);
    oa2.setDuration(2000);
    oa2.setRepeatCount(1);
    oa2.setRepeatMode(ValueAnimator.REVERSE);

    ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 1.6f, 1.2f, 2f);
    oa3.setDuration(2000);
    oa3.setRepeatCount(1);
    oa3.setRepeatMode(ValueAnimator.REVERSE);

    ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0f, 180f, 90f, 360f);
    oa4.setDuration(2000);
    oa4.setRepeatCount(1);
    oa4.setRepeatMode(ValueAnimator.REVERSE);

    // set.playSequentially(oa1, oa2, oa3, oa4);   // 依序播放
    set.playTogether(oa1, oa2, oa3, oa4);         // 同時播放
    set.start();
}

屬性動畫同樣可以寫在 XML 中,再於程式碼載入並指定作用目標:

public void xml(View v) {
    Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator);
    at.setTarget(iv);
    at.start();
}

選擇哪一種,取決於需求:只是短暫的視覺提示(例如淡入淡出、按壓回饋),補間動畫更省事;開場或精緻的序列畫面用幀動畫;而需要「動完真的位移了」的情境,就該用屬性動畫。另外,補間動畫不改變真正的屬性值這點常 causing 後續點擊判定錯位,實務上要特別小心。

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