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

Android 動畫入門:四種基本效果的程式碼實作

編輯:關於 Android 程式設計

Android 動畫 API 提供了四種基本效果:縮放、平移、旋轉、透明度變化。這篇用純程式碼的方式把四種都跑一遍,理解參數含義之後,XML 寫法自然一通百通。

版面配置

用 RelativeLayout 依序排出一個 ImageView 與四個按鈕,每個按鈕觸發一種動畫:

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

<Button
    android:id="@+id/scaleButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/imgLayout"
    android:text="@string/scale" />

<Button
    android:id="@+id/translateButton"
    android:layout_below="@id/scaleButton"
    ... />

<Button
    android:id="@+id/rotateButton"
    android:layout_below="@id/translateButton"
    ... />

<Button
    android:id="@+id/alphaButton"
    android:layout_below="@id/rotateButton"
    ... />

ImageView 建議包在一個有 padding 的 LinearLayout 裡,這樣縮放與平移時才不會貼邊。

一、縮放:ScaleAnimation

class setScaleListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        AnimationSet animationSet = new AnimationSet(true);
        // 從 (1,1) 縮小到 (0.5,0.5)
        Animation animation = new ScaleAnimation(1f, 0.5f, 1f, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(1000);
        animation.setStartOffset(1000);
        animation.setRepeatCount(0);
        animation.setFillAfter(true);
        animationSet.addAnimation(animation);
        imageView.startAnimation(animation);
    }
}

ScaleAnimation 有六個參數,前四個是 X 與 Y 的起訖比例,後四個是縮放中心的定位方式與位置。這裡用 RELATIVE_TO_SELF 搭配 0.5,表示以自身中心為縮放點——若寫成 RELATIVE_TO_PARENT,同樣的 0.5 就會變成父容器的中心,效果完全不同。

setStartOffset(1000) 讓動畫延遲一秒才開始;setFillAfter(true) 讓動畫結束後停在最終狀態,不會跳回原樣。這兩個是實務上最常用的設定。

二、平移:TranslateAnimation

class setTranslateListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        AnimationSet animationSet = new AnimationSet(true);
        // 從 (0,0) 移到 (1,1)
        Animation animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
        animation.setDuration(1000);
        animationSet.addAnimation(animation);
        imageView.startAnimation(animation);
    }
}

參數依序是:起點 X 的類型與值、終點 X 的類型與值、起點 Y 的類型與值、終點 Y 的類型與值。類型可選 RELATIVE_TO_SELF(自身比例)、RELATIVE_TO_PARENT(父容器比例)或絕對像素值。

三、旋轉:RotateAnimation

class setRotateListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        AnimationSet animationSet = new AnimationSet(true);
        // 0 度轉到 360 度,以自身底部中心為軸
        Animation animation = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1f);
        animation.setDuration(1000);
        animationSet.addAnimation(animation);
        imageView.startAnimation(animation);
    }
}

五個參數:起始角度、結束角度、X 軸類型與值、Y 軸類型與值。正值為順時針,負值為逆時針。旋轉軸的設定邏輯與縮放中心相同。

四、透明度:AlphaAnimation

class setAlphaListener implements OnClickListener {
    @Override
    public void onClick(View v) {
        AnimationSet animationSet = new AnimationSet(true);
        // 1 代表完全可見,0 代表完全透明
        Animation animation = new AlphaAnimation(1, 0);
        animation.setDuration(1000);
        animationSet.addAnimation(animation);
        imageView.startAnimation(animation);
    }
}

兩個參數分別是起始與結束的透明度。1 到 0 是淡出,0 到 1 是淡入。

幾個容易踩的細節

第一,範例中每個監聽器都建立了 AnimationSet,卻只把單一動畫加進去,最後呼叫 imageView.startAnimation(animation) 啟動的是動畫本身而非集合。若確實要組合多個動畫,應該啟動 animationSet,並注意 AnimationSet 的建構子參數 true 表示共用同一個插值器。

第二,setRepeatCount 若要無限循環可設為 Animation.INFINITE,搭配 setRepeatMode 決定是重播(RESTART)還是來回(REVERSE)。

第三,View 動畫只改變繪製效果,不真正改變 View 的位置與狀態。也就是說平移之後,View 的實際點擊區域仍留在原位,這是初學者最常遇到的困惑。若需要真正的位置變更,應該用属性動畫(ObjectAnimator / ValueAnimator)。

第四,記得在 MainActivity 的 onCreate 中把四個按鈕與對應監聽器綁定,並透過 findViewById 取得 myImage 參照。

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