Android學習(十六) 通過GestureDetector進行手勢識別

來源:互聯網
上載者:User

標籤:

一、手勢互動過程:

  1)觸屏時,觸發MotionEvent事件。

  2)被OnTouchListener監聽,在onTouch()中獲得MotionEvent對象。

  3)GestureDetector轉寄MotionEvent對象至OnGestureListener。

  4)OnGestureListener獲得該對象,根據該對象封裝的資訊做出合適的反饋。

 

二、需要用到的類和介面

  1、MotionEvent:

  1)用於封裝手勢、觸摸筆,軌跡球等動作事件。

  2)內部封裝用於記錄橫軸和縱軸座標的屬性X和Y。

  

  2、GestureDetector:

  識別各種手勢:按下,移動,彈起。

 

  3、OnGestureListener介面

  1)手勢互動的監聽介面,其提供多個抽象方法。

    a) onDown(MotionEvent e);   //單擊

    b) onSingleTapUp(MotionEvent e);  //抬起

    c) onShowPress(MotionEvent e);   //短按

    d) onLongPress(MotionEvent e);   //長按

    e) onScoll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)  //滾動

    f) onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)    //滑動

 

  2)根據GestureDetector的手勢識別結果調用相對應的方法。

 

  4、OnDoubleTapListener介面:

    a) onDoubleTap(MotionEvent e);   //雙擊

    b) onDoubleTapEvent(MotionEvent e);  //雙擊按下和抬起各觸發一次

    c) onSingleTapConfirmed(MotionEvent e);  //單擊確認,很快的按下並彈起,但不點擊第二下。

 

  5、SimpleOnGesttureListener類,實現了OnGestureListener和OnDoubleTapListener介面,如果我們需要實現手勢,只需要繼承這個類,實現對應的手勢方法即可。

    

樣本:根據左右拖拽切換圖片,直接實現介面

main.xml,添加一個ImageView顯示圖片,預設顯示第一張圖片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/img_girl"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/girl1" /></LinearLayout>

 

main.java

package com.example.gesturedetectordemo;import android.os.Bundle;import android.app.Activity;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.Menu;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {    //定義一個GestureDetector對象,用來處理各種手勢,實現了OnTouchListener介面    GestureDetector myGestureDetector;    // 圖片框    ImageView imggirl;    // 存放女孩圖片的數組    int[] girls = { R.drawable.girl1, R.drawable.girl2, R.drawable.girl3, R.drawable.girl4, R.drawable.girl5 };    // 存放數組下標    private int index;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        myGestureDetector = new GestureDetector(this);        imggirl = (ImageView) findViewById(R.id.img_girl);        //添加OnTouchListener事件        imggirl.setOnTouchListener(this);        //下面兩個要記得設哦,不然就沒法處理輕觸以外的事件了,例如拋擲動作。        imggirl.setLongClickable(true);         myGestureDetector.setIsLongpressEnabled(true);     }    // 向後移動    public void goNext() {        index++;        index = Math.abs(index % girls.length);        imggirl.setImageResource(girls[index]);    }    // 向前移動    public void goPrevious() {        index--;        index = Math.abs(index % girls.length);        imggirl.setImageResource(girls[index]);    }    @Override  //處理使用者的觸碰請求    public boolean onTouch(View v, MotionEvent event) {        //轉交給GestureDetector來處理        myGestureDetector.onTouchEvent(event);        return false;    }    @Override  //在按下動作時被調用     public boolean onDown(MotionEvent e) {        //goNext();        return false;    }    @Override  //按下動作鬆開被調用    public void onShowPress(MotionEvent e) {        // TODO Auto-generated method stub    }    @Override  //在彈起時被調用    public boolean onSingleTapUp(MotionEvent e) {        // TODO Auto-generated method stub        return false;    }    @Override  //在滾動時被調用    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {        // TODO Auto-generated method stub        return false;    }    @Override   //在長按時被調用    public void onLongPress(MotionEvent e) {        // TODO Auto-generated method stub    }    @Override  //在拋擲動作時被調用     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {        if (e1.getX() - e2.getX() > 50) {            goNext();            Toast.makeText(MainActivity.this, "從右向左拖拽" + index, Toast.LENGTH_SHORT).show();        } else if (e2.getX() - e1.getX() > 50) {            goPrevious();            Toast.makeText(MainActivity.this, "從左向右拖拽" + index, Toast.LENGTH_SHORT).show();        }        return false;    }}

 

 

 

  

Android學習(十六) 通過GestureDetector進行手勢識別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.