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

Android 中使用 Animation 實作動畫效果與 Interpolator、AnimationListener 用法

編輯:關於 Android 程式設計

在 Android 中,Animation 為元件提供了豐富的動畫能力。本文將介紹四種基本動畫,並說明如何透過 Interpolator 控制變化節奏、以 AnimationListener 監聽動畫事件,以及逐幀動畫與 LayoutAnimationController 的應用。

一、AlphaAnimation:淡入淡出

以程式碼實作:

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(1000);
alphaAnimation.setStartOffset(10000);
animationSet.addAnimation(alphaAnimation);
animationSet.setFillBefore(false);
animationSet.setFillAfter(true);
imageView.startAnimation(animationSet);

以 XML 實作,先在 res/anim 建立 alpha.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fillAfter="true"
    android:fillBefore="false">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="1000"
        android:duration="1000" />
</set>

再於 Activity 中載入:

Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);

二、ScaleAnimation:縮放效果

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
        Animation.RELATIVE_TO_SELF, 1f,
        Animation.RELATIVE_TO_SELF, 1f);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(1000);
imageView.startAnimation(animationSet);

對應的 XML(scale.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <scale android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000" />
</set>

三、RotateAnimation:旋轉效果

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_PARENT, 0.5f,
        Animation.RELATIVE_TO_PARENT, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);

對應的 XML(rotate.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <rotate android:fromDegrees="0"
        android:toDegrees="+360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000" />
</set>

四、TranslateAnimation:移動效果

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 1.0f,
        Animation.RELATIVE_TO_SELF, 0f,
        Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);

對應的 XML(translate.xml),其中 100%p 表示相對於父容器的位置:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="100%p"
        android:fromYDelta="0%p"
        android:toYDelta="100%p"
        android:duration="1000" />
</set>

組合多個動畫

可透過 AnimationSet 為單一元件添加多個動畫,程式碼寫法:

AnimationSet animationSet = new AnimationSet(false);
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(alpha);
animationSet.addAnimation(scale);
animationSet.setDuration(2000);
animationSet.setStartOffset(1000);
animationSet.setFillAfter(true);
imageView.startAnimation(animationSet);

XML 寫法(alpha.xml):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true"
    android:fillAfter="true">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="1000"
        android:fillAfter="true"
        android:duration="2000" />
    <scale android:fromXScale="1.0"
        android:toXScale="0.5"
        android:fromYScale="1.0"
        android:toYScale="0.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="1000"
        android:duration="2000" />
</set>

Interpolator 的運用

Interpolator 定義了動畫變化的速率或規律,常見的子類包含:

AccelerateDecelerateInterpolator  開始與結束較慢,中段加速
AccelerateInterpolator            起步慢而後加速
CycleInterpolator                 以正弦規律重複指定次數
DecelerateInterpolator            起步快而後減速
LinearInterpolator                等速變化

可在 set 標籤中統一設定,或為每個動畫標籤分別指定:

// set 標籤中定義
<set ...
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true" ...>

// 於程式碼中設定
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());

// 或分別設定
AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setInterpolator(new AccelerateInterpolator());
ScaleAnimation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f,
    Animation.RELATIVE_TO_SELF, 0.5f);
scale.setInterpolator(new AccelerateDecelerateInterpolator());

逐幀動畫(Frame-By-Frame)

先準備好 run1.png ~ run4.png 四張圖片放入 drawable 目錄,建立 anim_run.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/run1" android:duration="100" />
    <item android:drawable="@drawable/run2" android:duration="100" />
    <item android:drawable="@drawable/run3" android:duration="100" />
    <item android:drawable="@drawable/run4" android:duration="100" />
</animation-list>

於 Activity 中設定背景並啟動動畫:

imageView.setBackgroundResource(R.drawable.anim_run);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();

LayoutAnimationController 控制 ListView

先建立控制動畫的 list_anim.xml,再於版面配置中加入 ListView,最後透過程式碼或 XML 設定:

// 程式碼方式
Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.list_anim);
LayoutAnimationController lac = new LayoutAnimationController(animation);
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
lac.setDelay(0.5f);
listView.setLayoutAnimation(lac);

// XML 方式:建立 anim_layout.xml
<layoutAnimation xmlns:android="https://schemas.android.com/apk/res/android"
    android:delay="1"
    android:animationOrder="normal"
    android:animation="@anim/list_anim" />

AnimationListener 監聽動畫事件

可為元件設定監聽器,覆寫三個回呼方法:

private class RemoveAnimationListener implements AnimationListener{
    @Override
    public void onAnimationEnd(Animation animation) {
        // 動畫結束後刪除某個 TextView
        viewGroup.removeView(textView);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        System.out.println("onAnimationRepeat");
    }

    @Override
    public void onAnimationStart(Animation animation) {
        System.out.println("onAnimationStart");
    }
}

透過監聽器,就能在動畫開始、重複與結束的各個階段插入自訂邏輯,例如完成淡出後移除元件,或反之在動畫中動態加入元件。

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