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

Android 動畫系統之視圖動畫(View Animation)入門與實戰

編輯:關於 Android 程式設計

視圖動畫的適用場景與局限

視圖動畫(又稱補間動畫)是最早的一套動畫方案,使用簡單但有明確邊界:

  1. 只能為 View 添加動畫。若想讓非 View 對象动起来,只能自己實現。
  2. 能操作的屬性很有限。它暴露了 View 的縮放與旋轉等能力,卻不支持背景色變化之類的需求。
  3. 它改變的只是繪製效果,View 的真實屬性並未變動。比如把一個 Button 縮放得很小,可點擊區域依然是原來那麼大。
  4. 但它上手極快、代碼量極少。如果需求它能滿足,就沒有必要上屬性動畫。

視圖動畫一共四種:縮放 ScaleAnimation、平移 TranslateAnimation、漸變 AlphaAnimation、旋轉 RotateAnimation;再加上一個用來組合它們的 AnimationSet,基本覆蓋了日常需求。

先記住一個常用開關:

animation.setFillAfter(true); // 動畫結束後保持終态,不回彈到起始畫面

一、縮放 ScaleAnimation

(1)以 View 左上角為縮放中心,水平放大一倍、垂直縮小為一半:

float fromXScale = 1.0f, toScaleX = 2.0f;
float fromYScale = 1.0f, toScaleY = 0.5f;
Animation animation = new ScaleAnimation(fromXScale, toScaleX, fromYScale, toScaleY);
animation.setDuration(3000);
textView.startAnimation(animation);

(2)以 View 中心為縮放中心,橫縱都縮小一半:

float pivotX = textView.getWidth() / 2;
float pivotY = textView.getHeight() / 2;
Animation animation = new ScaleAnimation(
        1.0f, 0.5f, 1.0f, 0.5f, pivotX, pivotY);
animation.setDuration(3000);
textView.startAnimation(animation);

(3)用相對值指定中心,效果同上,但不必依賴 getWidth/getHeight:

int pivotXType = Animation.RELATIVE_TO_SELF;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_SELF;
float pivotYValue = 0.5f;
Animation animation = new ScaleAnimation(
        1.0f, 0.5f, 1.0f, 0.5f, pivotXType, pivotXValue, pivotYType, pivotYValue);
animation.setDuration(3000);
textView.startAnimation(animation);

二、平移 TranslateAnimation

(1)以像素為單位的構造方法:

int fromXDelta = 0;
int toXDelta = getResources().getDisplayMetrics().widthPixels / 2;
int fromYDelta = 0, toYDelta = 0;
Animation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
animation.setDuration(5000);
textView.startAnimation(animation);

四個參數分別是動畫開始/結束時,View 相對原位置在 X、Y 方向的偏移量。

(2)帶類型標識的構造方法,靈活性更高:

int fromXType = Animation.ABSOLUTE;
float fromXValue = textView.getX();
int toXType = Animation.RELATIVE_TO_PARENT;
float toXValue = 0.5f;
int fromYType = Animation.ABSOLUTE;
float fromYValue = textView.getY();
int toYType = Animation.RELATIVE_TO_SELF;
float toYValue = 3.0f;
Animation animation = new TranslateAnimation(
        fromXType, fromXValue, toXType, toXValue,
        fromYType, fromYValue, toYType, toYValue);
animation.setDuration(3000);
textView.startAnimation(animation);

類型標識決定了數值的含義,共三種:

  • ABSOLUTE:相對於父容器座標系的絕對值。例如 fromXValue 為 200,表示動畫開始時 View 左側距父容器左側 200 像素。
  • RELATIVE_TO_PARENT:相對於父容器尺寸的百分比。0 表示貼合父容器左側,0.5 表示位於水平中線,1 表示與父容器右側重合。
  • RELATIVE_TO_SELF:相對於自身尺寸的百分比。0 表示與初始位置相同,0.5 表示向右偏移半個自身寬度,1 表示向右偏移整整一個自身寬度。

三、漸變 AlphaAnimation

float fromAlpha = 0.0f; // 0.0 全透明,1.0 完全不透明
float toAlpha = 1.0f;   // 由全透明漸顯;反過來即為漸隱
Animation animation = new AlphaAnimation(fromAlpha, toAlpha);
animation.setDuration(5000);
textView.startAnimation(animation);

四、旋轉 RotateAnimation

(1)以 View 左上角為軸心,旋轉 60 度:

Animation animation = new RotateAnimation(0, 60);
animation.setDuration(3000);
textView.startAnimation(animation);

(2)以 View 自身中心為軸心,旋轉 90 度。這裡有個坑:View 在 getWidth() 返回 0 之前就取值,會拿到錯誤的中心點,因此需要放到 post 裡等佈局完成:

textView.post(new Runnable() {
    @Override
    public void run() {
        float pivotX = textView.getWidth() / 2;
        float pivotY = textView.getHeight() / 2;
        Animation animation = new RotateAnimation(0, 90, pivotX, pivotY);
        animation.setDuration(3000);
        textView.startAnimation(animation);
    }
});

(3)以父容器中心為軸心,旋轉一圈:

int pivotXType = Animation.RELATIVE_TO_PARENT;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_PARENT;
float pivotYValue = 0.5f;
Animation animation = new RotateAnimation(
        0, 360, pivotXType, pivotXValue, pivotYType, pivotYValue);
animation.setDuration(3000);
textView.startAnimation(animation);

五、三步搞定視圖動畫

  1. 創建 Animation 相應子類的實例;
  2. 調用 setDuration 設置時長(可選地再設置插值器、setFillAfter 等);
  3. 調用 View 的 startAnimation 啟動。

六、額外補充:AnimationSet 組合動畫

TranslateAnimation translateAnimation = new TranslateAnimation(0.1f, 100.0f, 0.1f, 100.0f);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);

AnimationSet set = new AnimationSet(true);
set.addAnimation(translateAnimation);
set.addAnimation(alphaAnimation);
set.setDuration(3000); // 該時長會作用到集合中的每個動畫
textView.startAnimation(set);

構造 AnimationSet 時傳入的 true 表示共用同一個插值器,多個動畫的節奏才會保持協調。

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