Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 手勢識別:用 ViewFlipper 做觸摸切換動畫

編輯:進階開發

我們先前介紹過「在 Android 開發中使用 Gallery 實作多級聯動」與「在 Android 中讓 Service 動態更新 UI」。這次要示範的是首頁左右拖動的效果。

實作起來並不複雜:用 ViewFlipper 把要來回切換的 View 裝在一起,再讓它與 GestureDetector 聯手判斷該顯示哪一頁,最後加上一點位移動畫即可。本例的規則是手指向左快滑切到下一頁、向右快滑切到上一頁,每頁放一張圖片。


Android ViewFlipper 效果

版面配置

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <ViewFlipper android:id="@+id/flipper"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <include android:id="@+id/firstlayout"  layout="@layout/first" />
        <include android:id="@+id/secondlayout" layout="@layout/second" />
        <include android:id="@+id/thirdlayout"  layout="@layout/third" />
        <include android:id="@+id/fourthlayout" layout="@layout/fourth" />
    </ViewFlipper>
</LinearLayout>

ViewFlipper 裡放了四個 layout,同一時間只會顯示其中一個,滑動時在這些 layout 之間移動。

每個 layout 都很簡單,就一張圖片:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:src="@drawable/v1" />
</LinearLayout>

Activity 的實作

Activity 需要實作兩個介面:OnGestureListener 與 OnTouchListener。完整程式碼如下,註解已標明各段用途。

package com.ideasandroid.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;

public class ViewFlipperDemo extends Activity implements OnGestureListener, OnTouchListener {

    private ViewFlipper mFlipper;
    GestureDetector mGestureDetector;
    private int mCurrentLayoutState;

    private static final int FLING_MIN_DISTANCE = 100;
    private static final int FLING_MIN_VELOCITY = 200;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mFlipper = (ViewFlipper) findViewById(R.id.flipper);
        // 註冊一個負責手勢識別的物件
        mGestureDetector = new GestureDetector(this);
        // 替 mFlipper 掛上 touch 監聽器
        mFlipper.setOnTouchListener(this);
        mCurrentLayoutState = 0;
        // 允許長按 ViewFlipper,這樣才識別得到拖動等手勢
        mFlipper.setLongClickable(true);
    }

    /**
     * 此方法在本例中未使用,可用來直接跳到指定頁面
     */
    public void switchLayoutStateTo(int switchTo) {
        while (mCurrentLayoutState != switchTo) {
            if (mCurrentLayoutState > switchTo) {
                mCurrentLayoutState--;
                mFlipper.setInAnimation(inFromLeftAnimation());
                mFlipper.setOutAnimation(outToRightAnimation());
                mFlipper.showPrevious();
            } else {
                mCurrentLayoutState++;
                mFlipper.setInAnimation(inFromRightAnimation());
                mFlipper.setOutAnimation(outToLeftAnimation());
                mFlipper.showNext();
            }
        }
    }

    /** 由右側進入的動畫 */
    protected Animation inFromRightAnimation() {
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromRight.setDuration(500);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
    }

    /** 向左側退出的動畫 */
    protected Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(500);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
    }

    /** 由左側進入的動畫 */
    protected Animation inFromLeftAnimation() {
        Animation inFromLeft = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromLeft.setDuration(500);
        inFromLeft.setInterpolator(new AccelerateInterpolator());
        return inFromLeft;
    }

    /** 向右側退出的動畫 */
    protected Animation outToRightAnimation() {
        Animation outtoRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoRight.setDuration(500);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
    }

    public boolean onDown(MotionEvent e) {
        return false;
    }

    /*
     * 使用者按下螢幕、快速移動後放開,即觸發這個事件
     * e1:第一個 ACTION_DOWN 事件
     * e2:最後一個 ACTION_MOVE 事件
     * velocityX:X 軸移動速度,像素/秒
     * velocityY:Y 軸移動速度,像素/秒
     * 觸發條件:X 軸位移大於 FLING_MIN_DISTANCE,且速度大於 FLING_MIN_VELOCITY
     */
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // 向左滑動
            mFlipper.setInAnimation(inFromRightAnimation());   // 進入時的動畫
            mFlipper.setOutAnimation(outToLeftAnimation());    // 退出時的動畫
            mFlipper.showNext();
        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // 向右滑動
            mFlipper.setInAnimation(inFromLeftAnimation());
            mFlipper.setOutAnimation(outToRightAnimation());
            mFlipper.showPrevious();
        }
        return false;
    }

    public void onLongPress(MotionEvent e) { }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    public void onShowPress(MotionEvent e) { }

    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    public boolean onTouch(View v, MotionEvent event) {
        // 一定要把觸屏事件交給手勢識別物件處理,自己算會很麻煩
        return mGestureDetector.onTouchEvent(event);
    }
}

幾個實作細節值得留意:mFlipper.setLongClickable(true) 這行不能省,否則 GestureDetector 收不到完整的移動軌跡;FLING_MIN_DISTANCE 與 FLING_MIN_VELOCITY 這兩個門檻要依實際手感調整,門檻太低會誤觸,太高則要滑得很用力;而動畫之所以用 TranslateAnimation 搭配 AccelerateInterpolator,是為了讓切頁有「被甩出去」的速度感,比線性位移自然得多。

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