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

Android Tween 動畫:平移、縮放、旋轉與透明度

編輯:關於 Android 程式設計

Android 中常見的動畫分為 Tween 動畫與 Frame 動畫。Tween 動畫主要對 View 進行平移、縮放、旋轉、透明度變化等操作,也可以將多種效果組合在一起。

透明度動畫

alpha 動畫可以讓 View 從不透明漸變到透明。

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="1"
        android:toAlpha="0" />
</set>

fromAlpha 表示起始透明度,toAlpha 表示結束透明度。1 表示不透明,0 表示完全透明。

縮放動畫

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <scale
        android:duration="1000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="2"
        android:toYScale="2"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>

pivotX 與 pivotY 表示縮放中心。設置為 50% 時,通常表示以 View 中心進行縮放。

旋轉動畫

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="180"
        android:pivotX="50%"
        android:pivotY="50%" />
</set>

fromDegrees 表示起始角度,toDegrees 表示結束角度。正值通常表示順時針旋轉,負值表示逆時針旋轉。

平移動畫

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:duration="1000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100%"
        android:toYDelta="0" />
</set>

fromXDelta 與 toXDelta 表示水平方向位移,fromYDelta 與 toYDelta 表示垂直方向位移。

組合動畫

如果希望同時執行多種效果,可以在同一個 set 中加入多個動畫節點。

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate ... />
    <alpha ... />
    <scale ... />
    <translate ... />
</set>

Activity 切換動畫

Activity 之間也可以使用動畫:

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);

Tween 動畫可以廣泛應用於按鈕反饋、頁面切換、菜單進場與引導動畫。掌握 alpha、scale、rotate、translate 四種基本動畫後,就可以組合出豐富的交互效果。

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