Android 手勢(shì)識(shí)別

原理:Android手勢(shì)識(shí)別其實(shí)和我們平常對(duì)觸摸事件的處理是一樣的,都是對(duì)MotionEvent事件進(jìn)行計(jì)算處理。

Android為我們提供了兩個(gè)基本的手勢(shì)識(shí)別。

  1. GestureDetector:單手勢(shì)識(shí)別,可以識(shí)別按下,單擊,雙擊,長(zhǎng)按,滑動(dòng),fling滑動(dòng)。
  2. ScaleGestureDetector:縮放手勢(shì)識(shí)別,

1. GestureDetector單手勢(shì)識(shí)別

GestureDetector中有兩個(gè)重要接口,

  1. OnGestureListener,識(shí)別單次點(diǎn)擊,滑動(dòng),fling滑動(dòng)的接口。
  2. OnDoubleTapListener: 識(shí)別雙擊回調(diào)的接口。

Android為了我們使用方便,給我們提供了SimpleOnGestureListener類,該類包含了上述兩個(gè)接口,我們可以根據(jù)需要選擇性實(shí)現(xiàn)方法。

eg:

GestureDetector gestureDetector = new GestureDetector(requireActivity(), new                    GestureDetector.SimpleOnGestureListener() {

            //按下時(shí)觸發(fā)
            public boolean onDown(MotionEvent e) { 
                return true;
            }

            //按下onDown很短時(shí)間沒(méi)有離開(kāi)觸發(fā),單純快速點(diǎn)擊不會(huì)觸發(fā),一般不使用該方法。
            public void onShowPress(MotionEvent e) { 
            }

            //觸摸抬起時(shí)觸發(fā),如果只是單純的單擊很快執(zhí)行順序?yàn)閛nDown-->onSingleTapUp,長(zhǎng)按不會(huì)觸發(fā)該事件
            public boolean onSingleTapUp(MotionEvent e) { 
                return false;
            }

            //滑動(dòng)觸發(fā)
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
                return false;
            }

            //快速滑動(dòng)后的Fling滑動(dòng), velocityX表示x方向速度
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
                return false;
            }

            //長(zhǎng)按觸發(fā),長(zhǎng)按的觸發(fā)順序?yàn)閛nDown-->onShowPress->onLongPress
            public void onLongPress(MotionEvent e) { 
            }

            //單擊確認(rèn)觸發(fā),發(fā)生在onSingleTapUp之后,抬起后才能確認(rèn)是否是單擊
            public boolean onSingleTapConfirmed(MotionEvent e) { 
                return false;
            }

            //雙擊確認(rèn)觸發(fā),
            public boolean onDoubleTap(MotionEvent e) { 
                return false;
            }

            //雙擊過(guò)程中發(fā)生的事件,一般不用
            public boolean onDoubleTapEvent(MotionEvent e) { 
                return false;
            }

        });

上面為單手勢(shì)實(shí)例化,使用時(shí)直接將觸摸事件傳遞進(jìn)GestureDetector即可,

gestureDetector.onTouchEvent(e);

單手勢(shì)識(shí)別源碼

2. ScaleGestureDetector縮放

縮放手勢(shì)識(shí)別使用方式和GestureDetector一樣,而且回調(diào)接口比較簡(jiǎn)單

ScaleGestureDetector gestureDetector = new ScaleGestureDetector(requireActivity(), new         
                                                                ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override   //開(kāi)始縮放
            public boolean onScaleBegin(ScaleGestureDetector detector) {
                return super.onScaleBegin(detector);
            }

            @Override   //縮放回調(diào)
            public boolean onScale(ScaleGestureDetector detector) {
                return super.onScale(detector);
            }

            @Override   //結(jié)束縮放
            public void onScaleEnd(ScaleGestureDetector detector) {
                super.onScaleEnd(detector);
            }
        });

縮放源碼

3. 自定義手勢(shì)識(shí)別

Android的手勢(shì)識(shí)別都是對(duì)觸摸的計(jì)算,可以根據(jù)自己需要的邏輯來(lái)自己定義識(shí)別,可以研究試下GestureDetector與ScaleGestureDetector源碼,

這里的源碼是別人寫(xiě)的,我這里只做引用使用。

自定義手勢(shì)識(shí)別源碼

4. 其他觸摸計(jì)算輔助類 VelocityTracker

VelocityTracker是用來(lái)計(jì)算滑動(dòng)速度的,通常用戶快速滑動(dòng)后我們會(huì)根據(jù)用戶的滑動(dòng)速度來(lái)再加一段自動(dòng)滑動(dòng)(Fling滑動(dòng))。使用方式也簡(jiǎn)單。各個(gè)手勢(shì)識(shí)別中一般也都用到它。

  1. 初始化。

    VelocityTracker tracker = VelocityTracker.obtain();
    
  2. 將觸摸事件添加到計(jì)算數(shù)據(jù)中。多次添加,一般每個(gè)觸摸事件都添加進(jìn)入

    tracker.addMovement(motionEvent);
    
  3. 獲得滑動(dòng)速度之前,先計(jì)算,計(jì)算之前添加的數(shù)據(jù)才有速度結(jié)果

    //1000 計(jì)算結(jié)果速度的時(shí)間,這里是1000ms多少像素, 5f表示想要得到的速度的最大值。
    tracker.computeCurrentVelocity(1000, 5f)
    
  4. 獲取滑動(dòng)速度

    tracker.getXVelocity();
    tracker.getYVelocity()
    
  5. 清除VelocityTracker中原有的數(shù)據(jù)

    tracker.clear(); //相當(dāng)于初始化之后的狀態(tài)
    
  6. 用完記得最后回收

    tracker.recycler();
    

VelocityTracker使用源碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容