Android Gesture 手势识别

手势识别

实现OnGestureListener, OnTouchListener接口

class MyView extend LinearLayout implements OnGestureListener, OnTouchListener {

    public MyView(Context context) {
        this.setOnTouchListener(this);// 将本类绑定触屏监听器
        GestureDetector gd = new GestureDetector(this);
    }
    
    //先经过gd.onTouchEvent(event)事件判断,
    //执行手势则不执行父类onTouch(View v, MotionEvent event)事件
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);
    }
    
    // --------------以下是使用OnGestureListener手势监听的时候重写的函数---------
    /**
     * @以下方法中的参数解释:
     * @e1:第1个是 ACTION_DOWN MotionEvent 按下的动作
     * @e2:后一个是ACTION_UP MotionEvent 抬起的动作(这里要看下备注5的解释)
     * @velocityX:X轴上的移动速度,像素/秒
     * @velocityY:Y轴上的移动速度,像素/秒
     */
    @Override
    public boolean onDown(MotionEvent e) {
        // ACTION_DOWN
        return false;
    }
    @Override
    // ACTION_DOWN 、短按不移动
    public void onShowPress(MotionEvent e) {
    }
    
    @Override
    // ACTION_DOWN 、长按不滑动
    public void onLongPress(MotionEvent e) {
    }
    
    @Override
    // ACTION_DOWN 、慢滑动
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        return false;
    }
    
    @Override
    // ACTION_DOWN 、快滑动、 ACTION_UP
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        v_str.add("onFling");
        //-------e1是MotionEvent.ACTION_DOWN, e2是MotionEvent.ACTION_UP----------
        // if(e1.getAction()==MotionEvent.ACTION_MOVE){
        // v_str.add("onFling");
        // }else if(e1.getAction()==MotionEvent.ACTION_DOWN){
        // v_str.add("onFling");
        // }else if(e1.getAction()==MotionEvent.ACTION_UP){
        // v_str.add("onFling");
        // }
        // if(e2.getAction()==MotionEvent.ACTION_MOVE){
        // v_str.add("onFling");
        // }else if(e2.getAction()==MotionEvent.ACTION_DOWN){
        // v_str.add("onFling");
        // }else if(e2.getAction()==MotionEvent.ACTION_UP){
        // v_str.add("onFling");
        // }
        return false;
    }
    @Override
    // 短按ACTION_DOWN、ACTION_UP
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
}

这只是一个简单的例子,Android Simples中有个完整的例子:Gestures Builder。

posted @ 2014-12-15 11:25  rfheh  阅读(338)  评论(0编辑  收藏  举报