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

Android 學習筆記:Tween 補間動畫的 Java 實現全解

編輯:關於 Android 程式設計

Android 的動畫體系主要分兩大類:Tween 動畫(補間動畫)與 Frame 動畫(逐幀動畫)。本文記錄 Tween 動畫的四個核心類、公用方法與完整示例。

Tween 的意思是「補間」——你只需描述「從什麼狀態變成什麼狀態」,中間的每一幀由系統插值計算。它能實現縮放、旋轉、平移、透明度變化這四類效果。

一、四個核心實現類

1. AlphaAnimation —— 透明度動畫

AlphaAnimation(float fromAlpha, float toAlpha)
  • fromAlpha:動畫開始時的透明度,取值 0.0 ~ 1.0
  • toAlpha:動畫結束時的透明度

2. ScaleAnimation —— 縮放動畫

ScaleAnimation(float fromX, float toX, float fromY, float toY,
               int pivotXType, float pivotXValue,
               int pivotYType, float pivotYValue)
  • fromX / toX:X 軸方向起始 / 結束的伸縮比例(相對於原始尺寸)
  • fromY / toY:Y 軸方向起始 / 結束的伸縮比例
  • pivotXType:X 軸伸縮模式,可取 Animation.ABSOLUTE(絕對值)、Animation.RELATIVE_TO_SELF(相對於自身)、Animation.RELATIVE_TO_PARENT(相對於父容器)
  • pivotXValue:X 軸縮放中心位置
  • pivotYType / pivotYValue:Y 軸的伸縮模式與中心位置

縮放效果好不好看,九成取決於中心點選得對不對。做「從按鈕中心放大」的效果,就用 RELATIVE_TO_SELF, 0.5f。

3. TranslateAnimation —— 平移動畫

TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
  • fromXDelta / toXDelta:X 軸起始 / 結束位置
  • fromYDelta / toYDelta:Y 軸起始 / 結束位置

這些值同樣可以是絕對像素,也可以用 Animation.RELATIVE_TO_SELF 這類常量修飾成比例值(0.0f ~ 1.0f)。

4. RotateAnimation —— 旋轉動畫

RotateAnimation(float fromDegrees, float toDegrees,
                int pivotXType, float pivotXValue,
                int pivotYType, float pivotYValue)
  • fromDegrees:起始角度
  • toDegrees:結束角度
  • 四個 pivot 參數含義與 ScaleAnimation 完全一致

二、四類動畫共用的方法

animation.setDuration(2000);        // 設置播放時長(毫秒)
animation.setRepeatCount(2);        // 設置重複次數(注意:是「額外重複」的次數)
animation.setRepeatMode(AlphaAnimation.REVERSE);  // 重複模式
iv.startAnimation(animation);       // 啟動動畫

重複模式有兩種:Animation.RESTART(每次都從頭開始)與 Animation.REVERSE(往返播放,第二遍倒著放)。做呼吸燈效果時 REVERSE 特別好用。

三、完整示例

佈局文件:五個按鈕分別觸發五種效果,下方一個 ImageView 作為動畫對象。

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ftf.tween.MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:onClick="click"
            android:text="透明度"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:onClick="click2"
            android:text="縮放"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:onClick="click3"
            android:text="旋轉"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:onClick="click4"
            android:text="平移"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:onClick="click5"
            android:text="組合"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

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

        <ImageView
            android:id="@+id/iv"
            android:src="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

Activity 代碼:

// 透明度變化
public void click(View view) {
    AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(2000);
    animation.setRepeatCount(2);
    animation.setRepeatMode(AlphaAnimation.REVERSE);
    iv.startAnimation(animation);
}

// 縮放
public void click2(View view) {
    ScaleAnimation animation = new ScaleAnimation(
            0.2f, 2.0f, 0.2f, 2.0f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(2000);
    animation.setRepeatCount(2);
    animation.setRepeatMode(AlphaAnimation.REVERSE);
    iv.startAnimation(animation);
}

// 旋轉
public void click3(View view) {
    RotateAnimation animation = new RotateAnimation(
            0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    animation.setDuration(2000);
    animation.setRepeatCount(2);
    animation.setRepeatMode(AlphaAnimation.REVERSE);
    iv.startAnimation(animation);
}

// 平移
public void click4(View view) {
    TranslateAnimation animation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.2f,
            Animation.RELATIVE_TO_PARENT, 1.0f,
            Animation.RELATIVE_TO_PARENT, 0.2f,
            Animation.RELATIVE_TO_PARENT, 1.0f);
    animation.setDuration(2000);
    animation.setRepeatCount(2);
    animation.setRepeatMode(AlphaAnimation.REVERSE);
    iv.startAnimation(animation);
}

// 組合動畫
public void click5(View view) {
    AnimationSet set = new AnimationSet(false);

    TranslateAnimation pa = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.2f,
            Animation.RELATIVE_TO_PARENT, 1.0f,
            Animation.RELATIVE_TO_PARENT, 0.2f,
            Animation.RELATIVE_TO_PARENT, 1.0f);
    pa.setDuration(2000);
    pa.setRepeatCount(2);
    pa.setRepeatMode(AlphaAnimation.REVERSE);

    RotateAnimation ra = new RotateAnimation(
            0, 360,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    ra.setDuration(2000);
    ra.setRepeatCount(2);
    ra.setRepeatMode(AlphaAnimation.REVERSE);

    ScaleAnimation sa = new ScaleAnimation(
            0.2f, 2.0f, 0.2f, 2.0f,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
    sa.setDuration(2000);
    sa.setRepeatCount(2);
    sa.setRepeatMode(AlphaAnimation.REVERSE);

    set.addAnimation(sa);
    set.addAnimation(ra);
    set.addAnimation(pa);

    iv.startAnimation(set);
}

四、幾個容易踩的坑

  • AnimationSet(false) 裡的布爾參數是 shareInterpolator。傳 false 表示每個子動畫保留自己的插值器;傳 true 則所有子動畫共用集合的插值器,效果會完全不同。
  • 動畫不改變真正的控件屬性。Tween 動畫只是「視覺上」變了,點擊事件的分佈區域還在原位。如果希望點擊區域跟著動,需要用 LayoutAnimation 或改用屬性動畫(ObjectAnimator)。
  • 動畫結束後會「彈回」。因為真實屬性沒變,動畫一結束控件就恢復原樣。想保持最終狀態可以設置 setFillAfter(true),但這只是視覺欺騙,真正要改狀態還是得用屬性動畫。
  • 也可以在 XML 裡定義動畫。在 res/anim/ 下寫好動畫描述文件,用 AnimationUtils.loadAnimation(context, R.anim.xxx) 加載,能讓動畫參數和業務代碼分離,便於設計走查。

最後說一下選型:Tween 動畫適合「一次性的視覺效果」,比如進場動畫、提示閃爍。如果需要讓一個 View 真正從 (0,0) 移動到 (100,100) 並能被點擊,或者需要複雜的連鎖與關鍵幀控制,應該用 Android 3.0 之後引入的屬性動畫(ObjectAnimator / ValueAnimator),它直接操作對象屬性,沒有上述那些「視覺與實際不一致」的問題。

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