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

ViewFlipper 與手勢控制項入門:自動輪播與滑動切換

編輯:關於 Android 程式設計

一、ViewFlipper 基礎

用途:多用於多個子界面之間的切換,例如圖片輪播、手勢滑動切換等場景。

繼承關係:它繼承自 ViewAnimator,可以通過 addView(View v) 掛載任意數量的子控制項。

常用方法:

flipper.startFlipping() —— 開始自動播放
flipper.stopFlipping() —— 停止自動播放
flipper.showPrevious() —— 顯示上一個
flipper.showNext() —— 顯示下一個
flipper.setInAnimation(Context, int) / setOutAnimation(Context, int) —— 設置進出動畫

示例一:自動輪播

布局文件 activity_first.xml 中放一個 ViewFlipper(內部三個 ImageView)以及「上一個 / 自動播放 / 下一個」三個按鈕。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ViewFlipper
        android:id="@+id/details"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:flipInterval="1000">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/aa" />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ab" />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ac" />
    </ViewFlipper>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:onClick="prev"
        android:text="上一個" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="auto"
        android:text="自動播放" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:onClick="next"
        android:text="下一個" />
</RelativeLayout>

Activity 中只需在三個回調裡調用相應方法:

public class ViewFlipperActivity extends Activity {

    private ViewFlipper flipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        flipper = (ViewFlipper) findViewById(R.id.details);
    }

    public void prev(View source) {
        flipper.setInAnimation(this, android.R.anim.slide_in_left);
        flipper.setOutAnimation(this, android.R.anim.slide_out_right);
        flipper.showPrevious();
        flipper.stopFlipping();
    }

    public void auto(View source) {
        flipper.setInAnimation(this, android.R.anim.slide_in_left);
        flipper.setOutAnimation(this, android.R.anim.slide_out_right);
        flipper.startFlipping();
    }

    public void next(View source) {
        flipper.setInAnimation(this, android.R.anim.slide_in_left);
        flipper.setOutAnimation(this, android.R.anim.slide_out_right);
        flipper.showNext();
        flipper.stopFlipping();
    }
}

若需要自定義切換效果,可在 res/anim/ 下編寫一對動畫:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="300" />
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="300" />
</set>

另一份 slide_out_right 只需把位移方向對調即可。

二、Gesture 手勢

手勢能力常用於屏幕滑動、圖片縮放、手勢密碼等交互。核心回調方法包括:

onDown(MotionEvent e):手指按下時觸發。
onShowPress(MotionEvent e):按下後尚未移動或抬起時觸發。
onSingleTapUp(MotionEvent e):輕點一下時觸發。
onScroll(e1, e2, distanceX, distanceY):在屏幕上拖動時觸發。
onLongPress(MotionEvent e):長按時觸發。
onFling(e1, e2, velocityX, velocityY):快速滑過屏幕時觸發,velocityX/Y 分別為橫縱向速度,這是最常用的一個。

示例二:滑動切換登錄與註冊界面

依然使用 ViewFlipper,內部放兩個 LinearLayout,分別承載登錄表單與註冊表單。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ViewFlipper
        android:id="@+id/ViewFlipper01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <!-- 登錄界面 -->
        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/Ll_tv_01"
                android:textSize="20dp" />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/Ll_edt_01" />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/Ll_edt_02" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/Ll_bt_01" />
        </LinearLayout>

        <!-- 註冊界面 -->
        <LinearLayout
            android:id="@+id/LinearLayout02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/TextView02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/L2_tv_01"
                android:textSize="20dp" />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/L2_edt_01" />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/L2_edt_02" />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/L2_edt_03" />
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/L2_bt_01" />
        </LinearLayout>
    </ViewFlipper>
</LinearLayout>

Activity 實現 GestureDetector.OnGestureListener,把 Activity 的 onTouchEvent 轉交給手勢探測器,並在 onFling 中依據起止 X 坐標判斷滑動方向:

public class SecondActivity extends Activity implements GestureDetector.OnGestureListener {

    private ViewFlipper flipper;
    private GestureDetector detector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        detector = new GestureDetector(this);
        flipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }

    public boolean onDown(MotionEvent e) { return false; }
    public void onShowPress(MotionEvent e) { }
    public boolean onSingleTapUp(MotionEvent e) { return false; }
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; }
    public void onLongPress(MotionEvent e) { }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float x, float y) {
        if (e1.getX() > e2.getX()) {
            flipper.showNext();      // 向左滑,顯示下一個
        } else if (e1.getX() < e2.getX()) {
            flipper.showPrevious();  // 向右滑,顯示上一個
        } else {
            return false;
        }
        return true;
    }
}

三、Gesture 的標準使用步驟

第一步:創建 GestureDetector 對象。創建時必須提供一個手勢監聽器實例,可以直接繼承 SimpleOnGestureListener,只重寫自己關心的方法:

private GestureDetector.OnGestureListener onGestureListener =
        new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }
        };
GestureDetector detector = new GestureDetector(MainActivity.this, onGestureListener);

第二步:重寫 Activity 的 onTouchEvent,把觸摸事件交給探測器處理。

@Override
public boolean onTouchEvent(MotionEvent event) {
    return detector.onTouchEvent(event);
}

剩下的就是在各個回調方法裡填寫自己的業務邏輯了。

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