當前位置:編程學習大全網 - 源碼下載 - android onTouchEvent和setOnTouchListener中onTouch的區別

android onTouchEvent和setOnTouchListener中onTouch的區別

觸摸事件分發機制,好好看看;

/blog/1944311

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 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方法

  • 上一篇:VB 如何獲取壹外部程序窗口內已知控件句柄的內容
  • 下一篇:RS485接口是什麽樣子的?
  • copyright 2024編程學習大全網