GestureDetector手势识别浅析

使用GestureDetector进行手势识别

手势交互过程

  1. 触屏时,触发MotionEvent事件
  2. 被OnTouchListener监听,在onTouch中获得MotionEvent对象
  3. GestureDetector转发MotionEvent对象给onGestureListener接口
  4. onGestureListener获得该对象,并根据对象封装的信息进行

MotionEvent 用于记录横轴和纵轴坐标的属性X和Y
GestureDetector 识别各种手势

  1. 当接收到触摸消息时,将消息交给GestureDetector加工处理
  2. 通过设置监听器获得GestureDetector处理后的手势

GestureDetector的监听器

OnGestureListener:处理单击类消息

  • onDown(MotionEvent e) 单击
  • onSingleTapUp(MotionEvent e) 抬起
  • onShowPress(MotionEvent e) 短按
  • onLongPress(MotionEvent e) 长按
  • onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)滚动
  • onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)滑动

OnDoubleTapListener:处理双击消息

  • onDoubleTap(MotionEvent e) 双击
  • onDoubleTapEvent(MotionEvent e) 双击按下和抬起各触发一次
  • onSingleTapConfirmed(MotionEvent e)很快的按下并抬起但不点击第二下

使用GestureOverlayView控件

  1. 通过安卓apidemo中的gesturesBuilder定义手势文件
  2. 将手势文件拷贝进自己项目里

Java代码:

// 加载手势库
    final GestureLibrary gestureLibrary = GestureLibraries.fromRawResource(
            MainActivity.this, R.raw.gestures);
    gestureLibrary.load();
OnGesturePerformedListener listener = new OnGesturePerformedListener() {
        @Override
        public void onGesturePerformed(GestureOverlayView overlay,
                Gesture gesture) {
            ArrayList<Prediction> recognize = gestureLibrary
                    .recognize(gesture);
            for (int i = 0; i < recognize.size(); i++) {
                Prediction prediction = recognize.get(i);
                if (prediction.score >= 8) {// 相识度score取值范围0-10
                    if (prediction.name.equals("sure")) {
                        Toast.makeText(MainActivity.this,
                                "手势为:" + prediction.name,
                                Toast.LENGTH_SHORT).show();
                    } else if (prediction.name.equals("pervious")) {
                        Toast.makeText(MainActivity.this,
                                "手势为:" + prediction.name,
                                Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "手势不存在",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };
    mGestureOverlayView.addOnGesturePerformedListener(listener);

手势源码详解参考:
Android手势源码详解参考:
手势的形成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值