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

用 ViewFlipper 實現 Activity 內的螢幕切換動畫

編輯:關於 Android 程式設計

這裡說的螢幕切換,指的是同一個 Activity 內部幾個畫面之間的轉場。ViewFlipper 繼承自 FrameLayout,搭配 ViewAnimator 就能為子 View 的切換加上動畫。先看成果:

與動畫相關的四個方法

  • setInAnimation:設定 View 進場動畫。有兩個版本,一個接收 Animation 物件,另一個接收 Context 與動畫資源 ID。
  • setOutAnimation:設定 View 退場動畫,參數同上。
  • showNext:顯示 FrameLayout 中的下一個 View。
  • showPrevious:顯示上一個 View。

先畫出座標概念

假設目前顯示的是圖二,要看圖一,就讓圖一由左向右進入,X 軸從 -100%p 移動到 0;要看圖三,則由右向左進入,X 軸從 100%p 變為 0。由於是橫向滑動,Y 軸不涉及。釐清起點與終點後,四個動畫檔就很直觀了(放在 res 下自建的 anim 目錄)。

四個 translate 動畫

in_leftright.xml — 由左向右進場:

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0"/>
</set>

out_leftright.xml — 由左向右離場:

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

in_rightleft.xml — 由右向左進場:

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

out_rightleft.xml — 由右向左離場:

<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p"/>
</set>

版面配置

直接把三張圖包成 LinearLayout 放進 ViewFlipper:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ViewFlipper
        android:id="@+id/vf"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/sample_1" />
        </LinearLayout>
        <!-- sample_2、sample_3 結構相同 -->
    </ViewFlipper>
</LinearLayout>

Activity 中的手勢判斷

public class FlipperActivity extends AppCompatActivity
        implements View.OnTouchListener {
    private ViewFlipper vf;
    float startX; // 手指按下時的 X 座標
    float endX;   // 手指抬起時的 X 座標

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewflipper_layout);
        vf = (ViewFlipper) findViewById(R.id.vf);
        vf.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = event.getX();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            endX = event.getX();
            if (startX > endX) {
                // 由右向左滑,圖片從右側進場
                vf.setInAnimation(this, R.anim.in_rightleft);
                vf.setOutAnimation(this, R.anim.out_rightleft);
                vf.showNext();
            } else if (startX < endX) {
                // 由左向右滑,圖片從左側進場
                vf.setInAnimation(this, R.anim.in_leftright);
                vf.setOutAnimation(this, R.anim.out_leftright);
                vf.showPrevious();
            }
        }
        return true;
    }
}

後記:座標軸差點想錯

剛開始我被動畫的呈現方式困擾了很久,後來索性畫了張座標圖,把每個動畫的起點終點標出來,瞬間豁然開朗。不過要提醒的是:Android 的螢幕座標系統和數學座標不同,原點在左上角且 Y 軸向下遞增。我最初畫的版本把原點放在左下角,經朋友提醒後查證才發現有誤,修正如下:

寫自定義動畫時,先把座標系搞清楚,能省下大量試錯時間。

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