Android自定义View

Android 滑动与手势GestureDetector

2017-11-07  本文已影响700人  安安zoe

onTouch 事件

GestureDetector

1. 手势识别GestureDetector

2. GestureDetector类

3. OnGestureListener

private class Mygesturelistener implements GestureDetector.OnGestureListener{  
    
    // 用户按下屏幕触发
    @Override
    public boolean onDown(MotionEvent e) {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    @Override
    public void onShowPress(MotionEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    // 点击轻抬起
    // 非常快的Touchup:onDown- onSingleTapUp -onSingleTapConfirmed
    // 稍微慢的Touchup:onDown - onShowPress - onSingleTapUp- onSingleTapConfirmed
    @Override
    public boolean onSingleTapUp(MotionEvent e) {  
        // TODO Auto-generated method stub  
        return false;  
    }  
    
    // 在屏幕上滑动或者拖动事件
    // 滑屏事件: onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
    // 拖动:onDown------》onScroll----》onScroll------》(onFling)(可能产生)---》ACTION_UP
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,  
            float distanceX, float distanceY) {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    // 长按屏幕 触发顺序:
    // onDown - onShowPress(按下时间超过短暂的时间) - onLongPress
    @Override
    public void onLongPress(MotionEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    // 滑屏 也叫作抛掷
    // 滑屏之前必定触发onScroll,但是onScroll不一定会触发onFling
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
            float velocityY) {  
        // TODO Auto-generated method stub  
        return false;  
    }       
}  

onScroll 动作结束后,抬起时的ACTION_UP 并没有被消耗掉,可以捕捉这一个MotionEvent进行相应的操作。

4. OnDoubleTapListener

private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  
    public boolean onSingleTapConfirmed(MotionEvent e) {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    public boolean onDoubleTap(MotionEvent e) {  
        // TODO Auto-generated method stub  
        return false;  
    }  
  
    public boolean onDoubleTapEvent(MotionEvent e) {  
        // TODO Auto-generated method stub  
        return false;  
    }  

5. SimpleOnGestureListener

6. 使用

GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);  

GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);  
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);  
TextView v = findViewById(R.id.textview);
v.setOnTouchListener(this);
v.setClickable(true); //需要设置
v.setLongClickable(true);// 需要设置

public boolean onTouch(View v,MotionEvent event){
  return mGestureDetector.onTouchEvent(event);
}

手势运动方向判断

1. GestureDetector.OnGestureListener类的onScroll方法参数 distanceX 和 distanceY问题

参数 含义
e1 The first down motion event that started the scrolling
e2 The move motion event that triggered the current onScroll.he move motion event that triggered the current onScroll.
distanceX The distance along the X axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
distanceY The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.

参考:

  1. 用户手势检测-GestureDetector使用详解
上一篇下一篇

猜你喜欢

热点阅读