Android 屏幕滑动事件

http://blog.csdn.net/iamfafa/article/details/6316062

Android里有两个类

  android.view.GestureDetector

  android.view.GestureDetector.SimpleOnGestureListener

  (另外android.widget.Gallery好像是更牛x的OnGestureListener )

  1)新建一个类继承SimpleOnGestureListener,HahaGestureDetectorListener

  可以实现以下event事件。

  boolean onDoubleTap(MotionEvent e)

  解释:双击的第二下Touch down时触发

  boolean onDoubleTapEvent(MotionEvent e)

  解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。

  boolean onDown(MotionEvent e)

  解释:Touch down时触发

  boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)

  解释:Touch了滑动一点距离后,up时触发。

  void onLongPress(MotionEvent e)

  解释:Touch了不移动一直Touch down时触发

  boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

  解释:Touch了滑动时触发。

  void onShowPress(MotionEvent e)

  解释:Touch了还没有滑动时触发

  (与onDown,onLongPress比较

  onDown只要Touch down一定立刻触发。

  而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。

  所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。

  )

  boolean onSingleTapConfirmed(MotionEvent e)

  boolean onSingleTapUp(MotionEvent e)

  解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。

  点击一下非常快的(不滑动)Touchup:

  onDown->onSingleTapUp->onSingleTapConfirmed

  点击一下稍微慢点的(不滑动)Touchup:

  onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

  2)在view的新建一个GestureDetector的对象。

  构造函数里 gestureDetector = new GestureDetector(new HahaGestureDetectorListener());

  然后在View的onTouchEvent里以下这样用,就可以在刚才1)弄的事件里写自己的代码了。

  @Override

  public boolean onTouchEvent(MotionEvent event) {

  gestureDetector.onTouchEvent(event);

  }

  但是,郁闷的是SimpleOnGestureListener没有提供onUp(), 还得在View的

  onTouchEvent的

  case MotionEvent.ACTION_UP:

  里来写对应的代码。

原文地址:https://www.cnblogs.com/bnuvincent/p/4590716.html