【asp源码 文章】【海纳源码下载】【入侵网站拿源码】keda源码

时间:2024-11-26 12:32:43 来源:cdn服务器源码 分类:知识

1.android onTouchEvent和setOnTouchListener中onTouch的区别

keda源码

android onTouchEvent和setOnTouchListener中onTouch的区别

       è§¦æ‘¸äº‹ä»¶åˆ†å‘机制,好好看看;

       /blog/

       Android中的事件分为按键事件和触摸事件,这里对触摸事件进行阐述。Touch事件是由一个ACTION_DOWN,n个

       ACTION_MOVE,一个ACTION_UP组成onClick,onLongClick,onScroll等事件。Android中的控件都是继承

       View这个基类的,而控件分为两种:一种是继承View不能包含其他控件的控件;一种是继承ViewGroup可以包含其他控件的控件,暂且称为容器控

       ä»¶ï¼Œæ¯”如ListView,GridView,LinearLayout等。

       è¿™é‡Œå…ˆå¯¹å‡ ä¸ªå‡½æ•°è®²è§£ä¸‹ã€‚

       Ø public boolean dispatchTouchEvent (MotionEventev) 这个方法分发TouchEvent

       Ø public booleanonInterceptTouchEvent(MotionEvent ev) 这个方法拦截TouchEvent

       Ø public boolean onTouchEvent(MotionEvent ev) 这个方法处理TouchEvent

       å…¶ä¸­view类中有dispatchTouchEvent和onTouchEvent两个方法,ViewGroup继承View,而且还新添了一个

       onInterceptTouchEvent方法。Activity中也无onInterceptTouchEvent方法,但有另外两种方法。我们可以

       å‘现上面3个方法都是返回boolean,那各代表什么意思呢?

        public boolean dispatchTouchEvent (MotionEvent ev)

        Activity中解释:

       Called to process touch screen

       events.You can override this 源码asp源码 文章to intercept all touch screen events before

        they aredispatched to the window. Be sure to call this implementation

       for touch screenevents that should be handled normally.

       Parameters

       ev

       The touch screen event.

       Returns

       Â· boolean Return true if this event was consumed.

       å®ƒä¼šè¢«è°ƒç”¨å¤„理触摸屏事件,可以重写覆盖此方法来拦截所有触摸屏事件在这些事件分发到窗口之前。通常应该处理触摸屏事件,一定要调用这个实现。当返

       å›žå€¼ä¸ºtrue时,表示这个事件已经被消费了。例如在TextActivity中dispatchTouchEvent在ACTION_MOVE返回

       true,运行结果如下:

       ä¹Ÿå°±æ˜¯å®ƒå¹¶æ²¡æœ‰æŠŠé‚£ACTION_MOVE分发下去。

       public boolean onInterceptTouchEvent (MotionEvent ev)

       Implementthis

        method to intercept all touch screen motion events. This allows you

       towatch events as they are dispatched to your children, and take

       ownership of thecurrent gesture at any point.

       Usingthis function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent),and

        using it requires implementing that method as well as this one in

       thecorrect way. Events will be received in the following order:

       1. You will receive the down event here.

       2. The

        down event will be handled either by a child of this viewgroup, or

       given to your own onTouchEvent() method to handle; this means youshould

       implement onTouchEvent() to return true, so you will continue to see

       therest of the gesture (instead of looking for a parent view to handle

       it). Also,by returning true from onTouchEvent(), you will not receive

       any followingevents in onInterceptTouchEvent() and all touch processing

       must happen inonTouchEvent() like normal.

       3. For

        as long as you return false from this function, eachfollowing event (up

        to and including the final up) will be delivered first hereand then to

       the target's onTouchEvent().

       4. If

        you return true from here, you will not receive any followingevents:

       the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to youronTouchEvent() method and no longer appear here.

       Parameters

       ev

       The motion event being dispatched down the hierarchy.

       Returns

       Â· Return

        true to steal motionevents from the children and have them dispatched

       to this ViewGroup throughonTouchEvent(). The current target will receive

        an ACTION_CANCEL event, and nofurther messages will be delivered here.

       åŸºæœ¬æ„æ€å°±æ˜¯ï¼š

       1. ACTION_DOWN首先会传递到onInterceptTouchEvent()方法

       2.如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return false,那么后续的move, up等事件将继续会先传递给该ViewGroup,之后才和down事件一样传递给最终的目标view的onTouchEvent()处理。

       3.如果该ViewGroup的onInterceptTouchEvent()在接收到down事件处理完成之后return true,那么后续的move, up等事件将不再传递给onInterceptTouchEvent(),而是和down事件一样传递给该ViewGroup的onTouchEvent()处理,注意,目标view将接收不到任何事件。

       4.如果最终需要处理事件的view的onTouchEvent()返回了false,那么该事件将被传递至其上一层次的view的onTouchEvent()处理。

       5.如果最终需要处理事件的view的onTouchEvent()返回了true,那么后续事件将可以继续传递给该view的onTouchEvent()处理。

       Android touch事件传递机制:

       æˆ‘们可以看看android源代码:

       Activity.java中

       æš‚且不管onUserInteraction方法因为它只是一个空方法如果你没实现的话。getWindow().superDispatchTouchEvent(ev)。其中getWindow()返回的是PhoneWindow。

       PhoneWindow.java:

       æ­¤å‡½æ•°è°ƒç”¨super.dispatchTouchEvent(event),Activity的rootview是

       PhoneWindow.DecorView,它继承FrameLayout。通过super.dispatchTouchEvent把touch事件派

       å‘给各个Activity的是子view。同时我可以看到,如果子view拦截了事件,则不会执行onTouchEvent函数。

       ViewGroup.java中dispatchTouchEvent方法:

       ç”±äºŽä»£ç è¿‡é•¿è¿™é‡Œå°±ä¸è´´å‡ºæ¥äº†ï¼Œä½†ä¹ŸçŸ¥é“它返回的是

       return target.dispatchTouchEvent(ev);

       è¿™é‡Œtarget指的是所分发的目标,可以是它本身,也可以是它的子View。

       ViewGroup.java中的onInterceptTouchEvent方法:

       é»˜è®¤æƒ…况下返回false。即不拦截touch事件。

       View.java中的dispatchTouchEvent方法

       è¿™é‡Œæˆ‘们很清楚可以知道如果if条件不成立则dispatchTouchEvent的返回值是onTouchEvent的返回值

       View.java中的onTouchEvent方法