Android GestureDetector手势监听

     仅作摘要,便于回顾。

     我们知道Android中可以为组件设置onTouchListener(),来监听上面的onTouch()事件,但是onTouch()只能处理比较简单的事件,所以Android中有一个封装好的手势监听类GestureDetector,可以为该类配置相应的监听接口,如:SimpleOnGestureListener对象。问题的关键是,我们可以为自己的组件设置onTouchListener(),并在其中的onTouch()方法中将MotionEvent参数传递给GestureDetector.onTouchEvent(),即调用GestureDetector的onTouchEvent方法。

     另外这里我还遇到了一个问题,就是GestureDetector只能放在某个具体的组件中,比如:要监听一个ImageView上手势事件,需要将GestureDetector对象放到这个ImageView类中,而不是在Activity中调用ImageView.onTouchListener()中将MotionEvent传递给GestureDetector对象。否则只会触发ImageView的DOWN,SLOW_PRESS,LONG_PRESS事件,而没有触发诸如:SCROLL,FLING等事件。

猜你喜欢

转载自wly1028.iteye.com/blog/1733904