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

Android 動畫入門筆記:補間動畫與屬性動畫的完整用法

編輯:關於 Android 開發

Android 動畫入門筆記:補間動畫與屬性動畫的完整用法

這份筆記把 Android 兩大動畫體系一次整理清楚:先看傳統的補間動畫(Tween Animation),包括平移、旋轉、縮放、透明度與集合播放,以及 XML 配置方式;再談屬性動畫(Property Animation)如何用 ObjectAnimator 與 AnimatorSet 完成同樣的效果。

一、平移 Translate

fromXType 是水平播放的型別,fromXValue 是起始位置(0 表示當前位置);toXType 與 toXValue 是終點;Y 軸參數同理。

TranslateAnimation translate = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 2,
        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
translate.setDuration(3000);
translate.setRepeatCount(Animation.INFINITE);      // 無限循環
translate.setRepeatMode(Animation.REVERSE);        // 往返播放
imageView.setAnimation(translate);
translate.start();

二、旋轉 Rotate

/* fromDegrees 起始角度;toDegrees 結束角度;
   pivotXType/pivotYType 中心點型別,RELATIVE_TO_SELF 表示相對於自身;
   pivotXValue 0.5f 表示以自己的一半處作為軸心 */
RotateAnimation rotate = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(5000);
rotate.setRepeatCount(Animation.INFINITE);
rotate.setRepeatMode(Animation.RESTART);
imageView.setAnimation(rotate);
rotate.start();

三、縮放 Scale

/* fromX、toX、fromY、toY 為縮放比例起訖值,1 表示原始大小;
   後四個參數同樣用來指定縮放中心點 */
ScaleAnimation scale = new ScaleAnimation(1, 2, 1, 2,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(5000);
scale.setRepeatCount(Animation.INFINITE);
scale.setRepeatMode(Animation.RESTART);
imageView.setAnimation(scale);
scale.start();

四、透明度 Alpha

/* fromAlpha 起始透明度,toAlpha 結束透明度,1 為不透明、0 為全透明 */
AlphaAnimation alpha = new AlphaAnimation(1, 0);
alpha.setDuration(2000);
alpha.setRepeatCount(Animation.INFINITE);
alpha.setRepeatMode(Animation.REVERSE);
imageView.setAnimation(alpha);
alpha.start();

五、集合播放 AnimationSet

AnimationSet set = new AnimationSet();
set.addAnimation(scale);
set.addAnimation(rotate);
set.addAnimation(translate);
imageView.startAnimation(set);   // 多個動畫同時播放

六、用 XML 配置補間動畫

先在 res 目錄下建立 anim 資料夾,再新增 XML 檔:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="200%" android:toXDelta="200%"
        android:fromYDelta="-100%" android:toYDelta="200%"
        android:duration="4000" android:repeatCount="infinite"
        android:repeatMode="reverse" />
    <rotate android:fromDegrees="0" android:toDegrees="360"
        android:pivotX="50%" android:pivotY="50%"
        android:duration="4000" android:repeatCount="infinite"
        android:repeatMode="reverse" />
</set>

數值若是純數字代表絕對值;若是百分比,代表自身寬高的倍數;百分比加上 p(如 50%p)則代表父元素的寬高倍數。

載入使用:

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

七、屬性動畫(Property Animation)

實務上多用程式碼控制。平移:

public void translate(View v) {
    /* 參數一:作用對象;參數二:屬性名;參數三:動畫數值起訖 */
    ObjectAnimator animX = ObjectAnimator.ofFloat(imageView, "translationX", -50, 50);
    ObjectAnimator animY = ObjectAnimator.ofFloat(imageView, "translationY", 0, 60);
    animX.setDuration(2000);
    animX.setRepeatCount(ObjectAnimator.INFINITE);
    animX.setRepeatMode(ObjectAnimator.REVERSE);
    animX.start();
}

旋轉(X、Y 軸依序播放):

public void rotate(View v) {
    ObjectAnimator rotateX = ObjectAnimator.ofFloat(imageView, "rotationX", 0, 360);
    ObjectAnimator rotateY = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360);
    rotateX.setDuration(3000);
    rotateX.setRepeatMode(Animation.REVERSE);
    rotateY.setDuration(3000);
    rotateY.setRepeatMode(Animation.REVERSE);
    AnimatorSet set = new AnimatorSet();
    set.playSequentially(rotateX, rotateY);   // 依序播放
    set.start();
}

縮放:

public void scale(View v) {
    ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2.0f, 5f);
    ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2.0f, 5f);
    scaleX.setDuration(3000);
    scaleX.setRepeatMode(Animation.REVERSE);
    scaleX.setRepeatCount(Animation.INFINITE);
    scaleY.setDuration(3000);
    scaleY.setRepeatMode(Animation.REVERSE);
    scaleY.setRepeatCount(Animation.INFINITE);
    AnimatorSet set = new AnimatorSet();
    set.playTogether(scaleX, scaleY);        // 同時播放
    set.start();
}

透明度:

public void alpha(View v) {
    ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0f);
    alpha.setDuration(3000);
    alpha.setRepeatMode(Animation.REVERSE);
    alpha.setRepeatCount(Animation.INFINITE);
    alpha.start();
}

集合:

public void set(View v) {
    ObjectAnimator animX = ObjectAnimator.ofFloat(imageView, "translationX", -50, 100);
    ObjectAnimator animY = ObjectAnimator.ofFloat(imageView, "translationY", -70, 100);
    animX.setDuration(2000);
    animX.setRepeatMode(ObjectAnimator.REVERSE);
    animY.setDuration(2000);
    animY.setRepeatMode(ObjectAnimator.REVERSE);
    AnimatorSet set = new AnimatorSet();
    // set.playTogether(...) 同時播放;playSequentially(...) 依序播放
    set.playSequentially(animX, animY);
    set.start();
}

若想用 XML 配置屬性動畫,在 res 下建立 animator 資料夾並新增 XML 檔,然後載入:

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator);
set.setTarget(myObject);
set.start();

最後記一點兩者的差別:補間動畫只是改變「繪製時的視覺效果」,View 的真實位置與狀態並沒有變,所以點擊事件仍然落在原本的位置;屬性動畫則是真的改動物件屬性,能正確觸發事件與後續狀態。這也是新專案普遍改用屬性動畫的主要原因。

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