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

Android View Animation 完整分類與常用方法詳解

編輯:Android 程式設計入門

一、Animations 概觀

Animations 是 Android 提供的一套 UI 動畫 API,能做旋轉、縮放、淡入淡出、位移等效果,且幾乎可以套用在所有控制項上。它是理解 Android 動畫體系的起點。

二、兩大分類

1. Tweened Animations(補間動畫)

提供旋轉、移動、伸展與淡出等效果,四種基本型別為:

  • Alpha——淡入淡出
  • Scale——縮放
  • Rotate——旋轉
  • Translate——位移

2. Frame-by-frame Animations(影格動畫)

建立一串 Drawable 序列,讓它們依指定的時間間隔逐格顯示,本質上就是播放動圖。適合角色行走這類複雜動作,但要注意圖片總量對記憶體的壓力。

三、使用步驟(程式碼方式)

Tweened Animation 的標準流程:

  1. 建立一個 AnimationSet 物件(Animation 的子類別)。
  2. 建立所需的各個 Animation 子物件。
  3. 依需求為每個 Animation 設定參數。
  4. 把 Animation 加入 AnimationSet。
  5. 呼叫控制項的 startAnimation() 啟動。

四個子類別分別是:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation。

四、通用方法

  • setDuration(long durationMillis)——設定動畫持續時間,單位毫秒。
  • setFillAfter(boolean fillAfter)——為 true 時,動畫結束後控制項停留在終態。
  • setFillBefore(boolean fillBefore)——為 true 時,動畫結束後控制項回到執行前的狀態。
  • setStartOffset(long startOffset)——設定動畫開始前的等待時間。
  • setRepeatCount(int repeatCount)——設定重複次數,可用 INFINITE 無限循環。
  • setRepeatMode(int repeatMode)——設定重複模式,REVERSE 為來回播放,RESTART 為每次都從頭開始。

五、AnimationSet 的特性

  1. 它是 Animation 的子類別。
  2. 一個 AnimationSet 可包含多個 Animation。
  3. 對 AnimationSet 設定的通用屬性(如 startOffset、duration),會被子動畫繼承。

六、Interpolator:控制變化速率

Interpolator 定義了動畫的速率曲線,框架內建幾種:

  • AccelerateDecelerateInterpolator:頭尾慢、中段快,最貼近自然物體運動。
  • AccelerateInterpolator:起步慢,之後持續加速。
  • DecelerateInterpolator:起步快,之後逐漸減速。
  • LinearInterpolator:全程等速。
  • CycleInterpolator:依正弦曲線循環播放指定次數。

設定方式

1. 在 set 標籤上設定

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">

2. 讓整組動畫共用一個 Interpolator

android:shareInterpolator="true"

3. 不共用,各自指定

此時要設 android:shareInterpolator="false",並在每個子動畫上分別加上 interpolator:

<alpha
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:startOffset="500"
    android:duration="500" />

4. 程式碼中讓整組共用

AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());

5. 程式碼中不共用

AnimationSet animationSet = new AnimationSet(false);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
rotateAnimation.setInterpolator(new DecelerateInterpolator());

七、完整實作範例

package com.example.thestudyanimationview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

    private Button rotateButton, scaleButton, alphaButton, translateButton;
    private ImageView iv_rotateanimation, iv_scaleanimation, iv_alphaanimation, iv_translateanimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rotateButton = (Button) findViewById(R.id.rotateButton);
        scaleButton = (Button) findViewById(R.id.scaleButton);
        alphaButton = (Button) findViewById(R.id.alphaButton);
        translateButton = (Button) findViewById(R.id.translateButton);

        iv_rotateanimation = (ImageView) findViewById(R.id.iv_rotateanimation);
        iv_scaleanimation = (ImageView) findViewById(R.id.iv_scaleanimation);
        iv_alphaanimation = (ImageView) findViewById(R.id.iv_alphaanimation);
        iv_translateanimation = (ImageView) findViewById(R.id.iv_translateanimation);

        rotateButton.setOnClickListener(this);
        scaleButton.setOnClickListener(this);
        alphaButton.setOnClickListener(this);
        translateButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rotateButton:    rotateAnimation();    break;
            case R.id.scaleButton:     scaleAnimation();     break;
            case R.id.alphaButton:     alphaAnimation();     break;
            case R.id.translateButton: translateAnimation(); break;
        }
    }

    public void rotateAnimation() {
        AnimationSet animationSet = new AnimationSet(true);
        // 參數1:起始角度  參數2:結束角度
        // 後四個參數決定旋轉圓心位置
        // 座標型別可選 ABSOLUTE(絕對)、RELATIVE_TO_SELF(相對自身)、RELATIVE_TO_PARENT(相對父容器)
        // 0.5f 搭配 RELATIVE_TO_SELF 即為自身寬高的一半
        RotateAnimation rotateAnimation = new RotateAnimation(
                0, 360,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        animationSet.addAnimation(rotateAnimation);
        iv_rotateanimation.startAnimation(animationSet);

        rotateAnimation.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation arg0) { }
            public void onAnimationRepeat(Animation arg0) { }
            public void onAnimationEnd(Animation arg0) {
                iv_rotateanimation.setVisibility(View.INVISIBLE);
            }
        });
    }

    public void scaleAnimation() {
        AnimationSet animationSet = new AnimationSet(true);
        // 參數1、2:x 軸起始與結束比例
        // 參數3、4:y 軸起始與結束比例
        // 參數5~8:縮放中心點
        ScaleAnimation scaleAnimation = new ScaleAnimation(
                0, 0.1f, 0, 0.1f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(1000);
        animationSet.addAnimation(scaleAnimation);
        iv_scaleanimation.startAnimation(animationSet);

        scaleAnimation.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation arg0) { }
            public void onAnimationRepeat(Animation arg0) { }
            public void onAnimationEnd(Animation arg0) {
                iv_scaleanimation.setVisibility(View.INVISIBLE);
            }
        });
    }

    public void alphaAnimation() {
        // 參數 true 表示整組共用 Animation 的 interpolator
        AnimationSet animationSet = new AnimationSet(true);
        // 從完全不透明漸變到全透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setDuration(500);
        animationSet.addAnimation(alphaAnimation);
        iv_alphaanimation.startAnimation(animationSet);

        alphaAnimation.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation arg0) { }
            public void onAnimationRepeat(Animation arg0) { }
            public void onAnimationEnd(Animation arg0) {
                iv_alphaanimation.setVisibility(View.INVISIBLE);
            }
        });
    }

    public void translateAnimation() {
        AnimationSet animationSet = new AnimationSet(true);
        // 參數1~2:x 軸起點  參數3~4:y 軸起點
        // 參數5~6:x 軸終點  參數7~8:y 軸終點
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        translateAnimation.setDuration(1000);
        animationSet.addAnimation(translateAnimation);
        iv_translateanimation.startAnimation(animationSet);

        translateAnimation.setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation arg0) { }
            public void onAnimationRepeat(Animation arg0) { }
            public void onAnimationEnd(Animation arg0) {
                iv_translateanimation.setVisibility(View.INVISIBLE);
            }
        });
    }
}

對應的版面配置:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.thestudyanimationview.MainActivity" >

    <LinearLayout android:id="@+id/ll_bts"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <Button android:id="@+id/rotateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋轉" />
        <Button android:id="@+id/scaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="縮放" />
        <Button android:id="@+id/alphaButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="淡入淡出" />
        <Button android:id="@+id/translateButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="移動" />
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <ImageView android:id="@+id/iv_rotateanimation"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/icon_pengyouquan" />
        <ImageView android:id="@+id/iv_scaleanimation"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/icon_qq" />
        <ImageView android:id="@+id/iv_alphaanimation"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/icon_qqkongjian" />
        <ImageView android:id="@+id/iv_translateanimation"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@drawable/weibologon" />
    </LinearLayout>
</LinearLayout>

八、該用 View Animation 還是屬性動畫

View Animation(也就是本文的 Tween Animation)只改變「繪製時的外觀」,並不改變 View 自身的屬性。這帶來一個長期的困擾:平移之後 View 的實際位置沒變,點擊判定仍在原處。若你需要真正的位移與互動,請改用 Android 3.0 之後提供的屬性動畫(ObjectAnimator、ValueAnimator)。View Animation 的價值在於簡單直覺,適合做進場、閃現這類純視覺回饋。

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