Android L(5.0)源码之手势识别OnTouchListener

在Activity中,因为要监听触摸屏的触摸事件和手势时间,所以该Activity必须实现OnTouchListener和OnGestureListener两个接口,并重写其中的方法。本人根据android 5.0的源代码总结下OnTouchListener

 

  首先,Android的事件处理机制是基于Listener(监听器)来实现的,比我们今天所说的触摸屏相关的事件,就是通 过onTouchListener。其次,所有View的子类都可以通过setOnTouchListener()、 setOnKeyListener()等方法来添加对某一类事件的监听器。第三,Listener一般会以Interface(接口)的方式来提供,其中 包含一个或多个abstract(抽象)方法,我们需要实现这些方法来完成onTouch()、onKey()等等的操作。这样,当我们给某个view设 置了事件Listener,并实现了其中的抽象方法以后,程序便可以在特定的事件被dispatch到该view的时候,通过callbakc函数给予适 当的响应。

  贴上view结构图:有源代码可知,类view中总共提供了12个内部接口,其中包括:OnKeyListenerOnTouchListener、OnHoverListener、OnGenericMotionListener、OnLongClickListener、OnDragListener、OnFocusChangeListener、OnClickListener、OnCreateContextMenuListener、OnSystemUiVisibilityChangeListener、OnAttachStateChangeListener、OnApplyWindowInsetsListener、

  贴上源代码:

 1     /**
 2      * Interface definition for a callback to be invoked when a touch event is
 3      * dispatched to this view. The callback will be invoked before the touch
 4      * event is given to the view.
 5      */
 6     public interface OnTouchListener {
 7         /**
 8          * Called when a touch event is dispatched to a view. This allows listeners to
 9          * get a chance to respond before the target view.
10          *
11          * @param v The view the touch event has been dispatched to.
12          * @param event The MotionEvent object containing full information about
13          *        the event.
14          * @return True if the listener has consumed the event, false otherwise.
15          */
16         boolean onTouch(View v, MotionEvent event);
17     }

 

posted @ 2015-01-12 19:01  lelelong  阅读(473)  评论(0编辑  收藏  举报