Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

Android 動畫教學:XML 與 Java 兩種定義方式與四類動畫參數全解析

編輯:初階開發

Android 的動畫既可以在 XML 中定義,也可以完全用程式碼產生。下面以 RotateAnimation 為例示範兩種寫法,其他類型(ScaleAnimation、AlphaAnimation、TranslateAnimation)的原理完全一樣,只是參數不同。

方法一:在 XML 中定義

在 res/anim 下建立 my_rotate_action.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">

    <rotate
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="3000" />

</set>

屬性說明:

interpolator:指定動畫的插入器,用來控制速度變化曲線。accelerate_decelerate 是「先加速後減速」,另有 accelerate(愈來愈快)、decelerate(愈來愈慢)、linear(等速)等常見選項。
fromDegrees:動畫起始時物件的角度。
toDegrees:動畫結束時的角度,正值代表順時針。
duration:動畫持續時間,單位為毫秒。

在 Java 中載入並套用,效果是點擊按鈕後 TextView 旋轉一週:

package com.ray.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class TestAnimation extends Activity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate_action);
        findViewById(R.id.TextView01).startAnimation(anim);
    }
}

方法二:直接在程式碼中定義

package com.ray.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.Button;

public class TestAnimation extends Activity implements OnClickListener {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.Button);
        btn.setOnClickListener(this);
    }

    public void onClick(View v) {
        Animation anim = new RotateAnimation(0.0f, 360.0f);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());
        anim.setDuration(3000);
        findViewById(R.id.TextView01).startAnimation(anim);
    }
}

兩種做法效果相同,選擇標準很實際:參數固定、設計師會一起參與調校的,寫在 XML 裡比較方便;參數需要依執行期條件計算(例如依目前角度決定旋轉量)的,就用程式碼。

四類動畫的參數對照

AlphaAnimation(透明度)

private AlphaAnimation myAnimation_Alpha;

// fromAlpha:起始透明度;toAlpha:結束透明度
// 0.0 表示完全透明,1.0 表示完全不透明
myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
myAnimation_Alpha.setDuration(5000);   // 持續 5000 毫秒

常見用法是淡入(0 → 1)與淡出(1 → 0)。

ScaleAnimation(縮放)

// 參數依序:fromX, toX, fromY, toY,
//          pivotXType, pivotXValue, pivotYType, pivotYValue
//
// 前四個是 X / Y 軸的伸縮比例:
//   0.0 表示收縮到沒有,1.0 表示正常無伸縮
//   小於 1.0 為收縮,大於 1.0 為放大
//
// 後四個決定縮放的「中心點」在哪裡
myAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);

myAnimation_Scale.setDuration(700);

這裡的 pivot(樞點)是最容易搞錯的部分。RELATIVE_TO_SELF 搭配 0.5f 代表「相對於自身尺寸的 50% 處」,也就是正中心;若想從左下角放大,就改成 X 用 0f、Y 用 1f。另外還有 RELATIVE_TO_PARENT(相對於父容器)與 ABSOLUTE(絕對像素座標)兩種型別。

TranslateAnimation(位移)

// 參數依序:fromXDelta, toXDelta, fromYDelta, toYDelta
myAnimation_Translate = new TranslateAnimation(0f, 200f, 0f, 0f);
myAnimation_Translate.setDuration(2000);

這裡的數值同樣可以用 Animation.RELATIVE_TO_SELF 搭配比例來表達,寫成 1.0f 就是「移動自身一個寬度」。

RotateAnimation(旋轉)

// 參數依序:fromDegrees, toDegrees,
//          pivotXType, pivotXValue, pivotYType, pivotYValue
myAnimation_Rotate = new RotateAnimation(0.0f, 350.0f,
        Animation.RELATIVE_TO_SELF, 0.5f,
        Animation.RELATIVE_TO_SELF, 0.5f);

myAnimation_Rotate.setDuration(3000);

旋轉同樣需要指定中心點,省略 pivot 參數的建構子預設以 (0, 0) 為中心,也就是物件的左上角,這往往不是你要的效果。

怎麼套用動畫

使用從 View 父類繼承而來的 startAnimation(),就能為 View 或其子類套用一個動畫效果:

view.startAnimation(anim);

若要讓動畫跑完後保持最後的視覺狀態,記得加上 anim.setFillAfter(true),否則物件會「跳回」原位。

一個重要的觀念釐清

上面這些屬於 View Animation(補間動畫),也就是 Tween Animation。它的特性是:只改變畫面的呈現,不真正改變物件的屬性。也就是說,一個按鈕被平移到別的位置之後,你點擊螢幕上的新位置,觸發的其實仍是它原本所在的區域。若你需要「移動後真的改到位置」,應該改用 Property Animation(API 11 起提供):

ObjectAnimator.ofFloat(view, "translationX", 0f, 300f).setDuration(2000).start();
view.animate().alpha(0f).setDuration(500);

Property Animation 直接操作物件的屬性值,動畫結束後狀態就是真的改變了,也更容易與其他動畫串接組合。新專案若要處理互動型動畫,建議直接採用這套 API;補間動畫則仍很適合「跑完就恢復原狀」的裝飾性效果。

此外,若動畫需要在版面配置的過程中觸發(例如新增項目時的進場效果),可以搭配 LayoutTransition 或在 LayoutManager 中設定,比手動呼叫 startAnimation 更省事。

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