GestureDetector手勢滑動識別

2021-07-28 23:48:37 字數 1703 閱讀 4558

手勢識別是我們日常使用電子物品中非常常見的乙個功能,這個功能大大提公升了使用者體驗的感受

手勢識別主要有兩種方法,一種是gesturedetector,另一種是gestureoverlayview

現在來研究下gesturedetector方法

1.手指觸屏的一瞬間,觸發motionevent事件

2.被ontouchlistener監聽,在ontouch()中獲得motionevent物件

3.gesturedetector**motionevent物件至ongesturelistener

4.ongesturelistener獲得該物件,根據該物件封裝的資訊做出合適的反饋

motionevent:

用於封裝手勢、觸控筆、軌跡球等動作事件

內部封裝用於記錄橫軸和縱軸座標的屬性x和y  

手勢的滑動相當於螢幕上的xy軸的座標的變化

gesturedetector:識別各種手勢

當手指:按下、移動、抬起等可通過過載ontouch或者設定setontouchlistener來監聽

原理:當接收到使用者觸控訊息時,將訊息交給gesturedetector加工,通過設定***獲得gesturedetector處理後的手勢

ongesturelistener:手勢互動的監聽介面,其提供多個抽象方法、根據gesturedetector的手勢識別結果呼叫相對應的方法 ,監聽單擊事件

ondoubletaplistener:監聽雙擊事件

ongesturelistener介面的方法太多,沒有辦法寫完,所以只需寫乙個類,實現所需要的方法即可

單擊:ondown、抬起:onsingletapup、短按:onshowpress、長按:onlongpress、滾動:onscroll、滑動:onfling

android提供了乙個類******ongesturelistener:我們只需繼承這個類,然後過載感興趣的手勢就行

例子**如下:

package com.gdut.gesturedetector;

import android.os.bundle;

import android.view.gesturedetector;

import android.view.layoutinflater;

import android.view.menu;

import android.view.menuitem;

import android.view.motionevent;

import android.view.view;

import android.view.gesturedetector.******ongesturelistener;

import android.view.view.ontouchlistener;

import android.view.viewgroup;

import android.widget.imageview;

import android.widget.toast;

import android.os.build;

public class mainactivity extends activity {

但是gesturedetector,有侷限,就是只能進行比較少而簡單的手勢操作。

而比較複雜或者自定義的手勢,就由gestureoverlayview實現

本文出自 「11828641」 部落格,請務必保留此出處

GestureDetector 手勢檢測

學習記錄自 android開發藝術探索 手勢檢測,用於輔助檢測使用者單擊 滑動 長按 雙擊等行為.如下 private final gesturedetector mgesturedetector public studyviewgesturedetector context context 單擊 ...

Android開發之手勢滑動(滑動手勢監聽)詳解

android開發之手勢滑動 滑動手勢監聽 詳解 在android應用中,經常需要手勢滑動操作,比如上下滑動,或左右方向滑動,處理手勢滑動通常有兩種方法 一種是單獨實現setontouchlistener 來,另一種是構建手勢探測器 第一種方法,就是在要實現滑動的view中,實現ontouchlis...

vue封裝手勢滑動

利用touchstart和touchend事件,判斷手指觸控螢幕的位置距離差,在有效的時間內,即可判斷滑動方向 handletouchstart touchend hanldetouchend slot view template export default methods hanldetouch...