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

Android 動畫三兄弟:幀動畫、補間動畫與屬性動畫完整對照

編輯:關於 Android 開發

Android 的動畫體系可以分成三大類,特性差異很大,選錯了會白忙一場。

一、幀動畫 FrameAnimation

把一系列圖片依序播放,形成動畫效果。本質上它是一個 Drawable——一群圖片的集合,可以像單張圖片一樣使用。

在 drawable 目錄下建立以 <animation-list> 為根節點的資源檔:

<animation-list android:oneshot="false">
    <item android:drawable="@drawable/img1" android:duration="100"/>
    <item android:drawable="@drawable/img2" android:duration="100"/>
    <item android:drawable="@drawable/img3" android:duration="100"/>
    <item android:drawable="@drawable/img4" android:duration="100"/>
</animation-list>

屬性說明:

  • oneshot:是否只播放一次
  • drawable:這一幀引用的圖片
  • duration:這一幀播放的時間(毫秒)

播放方式是把動畫設為控制項背景後啟動:

((AnimationDrawable) view.getBackground()).start();

Animation 常用屬性:

  • duration:動畫時間
  • repeatCount:重複次數,infinite 代表無限次
  • fillAfter:是否停留在最後一幀
  • repeatMode:重複模式,restart 重新開始、reverse 來回反覆
  • startOffset:開始延遲時間

二、補間動畫 Tween Animation

只能應用於 View 物件,且只支援部分屬性。要特別注意:它只改變了 View 繪製的位置,並沒有改變物件本身的真實位置——這也是為什麼補間動畫的 View 常常「看得到點不到」。

可用 XML 定義(放在 /res/anim/)或用程式碼定義,透過 view.startAnimation(Animation a) 啟動。

AlphaAnimation:透明度

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="2000">
    <!-- fromAlpha 起始透明度:0 全透明、1 不透明
         toAlpha   結束透明度
         duration  動畫時間,單位毫秒 -->
</alpha>
AlphaAnimation alphaAnimation = null;
// 載入 XML 動畫
alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(
        MainActivity.this, R.anim.anim_alpha);

// 以下為各動畫通用的屬性設定
alphaAnimation.setRepeatCount(3);              // 結束後再重複 3 次,共 4 次
alphaAnimation.setRepeatMode(Animation.REVERSE); // 來回反覆
alphaAnimation.setFillAfter(true);             // 結束後停留在最後一幀
alphaAnimation.setFillBefore(false);           // 結束後不停留在第一幀
// 插值器:控制速度變化
alphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator());

ScaleAnimation:縮放

程式碼用法與 AlphaAnimation 完全相同,XML 如下:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromXScale="0.1"
    android:toXScale="1"
    android:fromYScale="0.1"
    android:toYScale="1"
    android:pivotX="0%"
    android:pivotY="0%"
    android:duration="2000">
    <!-- 各值為倍數
         fromXScale/toXScale:X 軸起始與結束縮放倍數
         fromYScale/toYScale:Y 軸起始與結束縮放倍數
         pivotX/pivotY:變換的基準點,0% 0% 即左上角原點 -->
</scale>

TranslateAnimation:位移

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromXDelta="-100%p"
    android:toXDelta="100%p"
    android:fromYDelta="0"
    android:toYDelta="0"
    android:duration="2000">
    <!-- 100%p 相對於父容器,100% 相對於自身 -->
</translate>

RotateAnimation:旋轉

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="https://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <!-- interpolator 可選:accelerate_decelerate(先加速後減速)、
         accelerate(加速)、decelerate(減速)
         fromDegrees 起始角度,toDegrees 結束角度(可大於 360)
         負數為逆時針,正數為順時針
         pivotX/pivotY:可為絕對值 100px,或 50%(自身)、50%p(父容器) -->
</rotate>

用建構子建立動畫

// 宣告
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;

// 初始化
myAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f);
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_Translate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
myAnimation_Rotate = new RotateAnimation(0.0f, +350.0f,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

各建構子參數:

  • AlphaAnimation(fromAlpha, toAlpha):起始與結束透明度,0.0 全透明、1.0 全不透明。
  • ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue):前四個為 X/Y 軸的起始與結束倍數(0.0 收到沒有、1.0 正常、小於 1 收縮、大於 1 放大),後四個決定變換基準點。
  • TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta):X/Y 軸的起始與結束位移量。
  • RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue):起始與結束角度,加上旋轉中心。
myAnimation_Alpha.setDuration(5000);   // 持續 5000 毫秒
myAnimation_Scale.setDuration(700);

三、屬性動畫 Property Animation

與補間動畫最大的差別:屬性動畫會真正改變目標物件的屬性值,而不只是視覺上的位移。代價是只能修改具有 getter/setter 的屬性。因為能改動真正的屬性,它能做到更多事:改文字大小、改背景顏色等等。

資源檔放在 res/animator。

ValueAnimator

包含屬性動畫的所有核心功能:動畫時間、起始與結束值、值的計算方式等。

使用流程是設定起始與結束值,實作 ValueAnimator.AnimatorUpdateListener(只有一個方法 onAnimationUpdate()),從傳入的 ValueAnimator 呼叫 getAnimatedValue() 取得目前值,再設定給某個控制項的某個屬性。

XML 定義:

<?xml version="1.0" encoding="utf-8"?>
<animator xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:valueFrom="0"
    android:valueTo="300"
    android:valueType="intType"
    android:interpolator="@android:interpolator/overshoot">
    <!-- valueType 可為 intType、floatType、colorType -->
</animator>

載入與啟動:

ValueAnimator valueAnimator = null;
// 透過 AnimatorInflater 載入 XML
valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(
        this, R.animator.animator_value);
valueAnimator.setDuration(3000);
valueAnimator.addUpdateListener(listener);
valueAnimator.start();
private ValueAnimator.AnimatorUpdateListener listener =
        new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        int value = (int) animation.getAnimatedValue();
        btnValueAnimator.setTranslationX(value);  // 水平位移
    }
};

用程式碼建立,並一次動多個屬性:

// 單一值
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator = ValueAnimator.ofInt(0, 300);
// 也能用來做顏色漸變(會呈現中間的過渡色)
valueAnimator = ValueAnimator.ofInt(Color.RED, Color.BLUE);
// 多屬性
PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
PropertyValuesHolder widthHolder = PropertyValuesHolder.ofInt("width", 0, 300);
valueAnimator = ValueAnimator.ofPropertyValuesHolder(alphaHolder, widthHolder);
valueAnimator.setDuration(3000);
valueAnimator.addUpdateListener(listener);
private ValueAnimator.AnimatorUpdateListener listener =
        new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // 多值時以名稱取出
        float alpha = (float) animation.getAnimatedValue("alpha");
        int width = (int) animation.getAnimatedValue("width");
        btnValueAnimator.setAlpha(alpha);
        // 改變寬度不能用 setWidth/setHeight,改右邊界更能觸發重新繪製
        btnValueAnimator.setRight(width);
    }
};

ObjectAnimator

繼承自 ValueAnimator,需要指定一個物件及其屬性;當值計算完成後會自動設定回該物件,不必自己掛監聽器,底層已經處理好。一般改動某個物件的某個屬性時都會用它。

Button btnObjectAnimator = (Button) findViewById(R.id.btn_object_animator);

// 載入 XML 動畫並綁定目標物件
ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(
        this, R.animator.animator_object);
objectAnimator.setTarget(btnObjectAnimator);

// 直接指定:參數1 目標物件、參數2 屬性名、參數3 值
objectAnimator = ObjectAnimator.ofInt(btnObjectAnimator, "textColor", Color.RED);

// 多屬性
PropertyValuesHolder translationXHolder =
        PropertyValuesHolder.ofFloat("translationX", 0, 300);
PropertyValuesHolder translationYHolder =
        PropertyValuesHolder.ofFloat("translationY", 0, 200);
objectAnimator = ObjectAnimator.ofPropertyValuesHolder(
        btnObjectAnimator, translationXHolder, translationYHolder);
objectAnimator.setDuration(3000);
objectAnimator.start();

怎麼選

要播放序列圖(例如 loading 轉圈、角色走路)就用幀動畫;只是讓 View「看起來」移動、旋轉、淡入淡出,用補間動畫最省事;但如果需要真的改變位置、尺寸或顏色,而且後續還要能點擊互動,就必須用屬性動畫——補間動畫在這裡會讓你踩到「位移後點不到」的坑。

轉載請註明出處:https://www.cnblogs.com/r-decade/

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