Android in dispatchtouchevent, onintercepttouchevent, ontouchevent understanding

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>"turn" http://blog.csdn.net/guitk/article/details/7057155</strong></p></p><p><p>Onintercepttouchevent is used to change the direction in which events are Passed. The return value that determines the direction of delivery is returned as false when the event is passed to the child control, and the return value is true when the event is passed to the current Control's ontouchevent (), which is known as intercept (intercept).</p></p><p><p><em>[tisa ps: the correct way to use this method is to only determine if an event needs to be intercepted and then Return. Even if interception is required, it should return true directly and then be processed by the Ontouchevent method. ]</em></p></p><p><p>Ontouchevent is used to handle events, and the return value determines whether the current control consumes (consume) the Event. In particular, for the Action_down event, returns true, indicating that I want to handle subsequent events; returns false, indicating that this event is not cared for and returned by the parent class for Processing.</p></p><p><p>Maybe you want to ask if there is a difference between consumption, anyway, I have written the processing code for the event? The answer is a difference! For example, Action_move or action_up occurrence of the premise is must have occurred action_down, if you do not consume action_down, then the system will think Action_down did not happen, so Action_ Move or action_up cannot be captured.</p></p><p><p>In cases where onintercepttouchevent () and ontouchevent () are not overridden (their return values are false), the order of the Motionevent events is passed as Follows:</p></p><p><p></p></p><p><p>When a Control's onintercepttouchevent () return value is true, truncation occurs, and the event is passed to the current Control's ontouchevent (). If we return the LAYOUTVIEW2 onintercepttouchevent () value to true, then the delivery process Becomes:</p></p><p><p></p></p><p><p>If we set LayoutView2 's onintercepttouchevent () and Ontouchevent () to True at the same time, then LAYOUTVIEW2 will consume the event that is passed, while subsequent events (such as following Action_ Down Action_move or Action_up) is passed directly to LayoutView2 's ontouchevent () and does not pass any function to any other Control. A Action_cancel event is passed to the subspace at the same time. The transfer process becomes (no Action_cancel event is drawn in the diagram):</p></p><p><p></p></p><p><p><em>[tisa ps: overall, onintercepttouchevent is from Rootview down, ontouchevent just the Opposite. ]</em></p></p><p><p></p></p><p><p><strong>"turn" http://blog.csdn.net/cyp331203/article/details/41039635</strong></p></p><p><p></p></p><p><p>The dispatchtouchevent (), onintercepttouchevent (), and Ontouchevent () methods are most important in the touch event delivery process in ANDROID. This is one of the problems that bothers beginners, and I started too. This records the process of dispatchtouchevent (), onintercepttouchevent (), and ontouchevent () for Memory.</p></p><p><p></p></p><p><p>Dispatchtouchevent is handling touch event distribution, and events (in most cases) start with the dispatchtouchevent of the Activity. Perform</p></p><p class="p1"><p class="p1"><span class="s1">Super.dispatchtouchevent (ev), The event is distributed Downward.</span></p></p><p><p><strong>Onintercepttouchevent</strong> is the method provided by the viewgroup, which returns false by default and returns true to indicate Interception.</p></p><p><p><strong>Ontouchevent</strong> is the method provided in view, ViewGroup also has this method, onintercepttouchevent is not available in VIEW. The default returns True in view, which indicates consumption of this Event.</p></p><p><p></p></p><p><p>In view, there are two callback functions:</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_1" width="16" height="16" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_1" width="+" height="" align="middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "><param name=" pluginspage " value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"> <param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always">< param name= "allowfullscreen" value= "false" ><param name="flashvars" value="id=1&width=16&height=16">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-j"> <ol class="dp-j"> <li class="alt"><span class="keyword">Public <span class="keyword">boolean dispatchtouchevent (motionevent ev);</span></span></li> <li><span class="keyword">Public <span class="keyword">boolean ontouchevent (motionevent ev); </span></span></li> </ol> </ol><p><p><em></em></p></p><p><p></p></p><p><p>In viewgroup, there are three callback functions:</p></p><p><p></p></p><strong><strong>[java]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_2" width="16" height="16" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_2" width="+" height="" align="middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "><param name=" pluginspage " value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"> <param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always">< param name= "allowfullscreen" value= "false" ><param name="flashvars" value="id=2&width=16&height=16">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-j"> <ol class="dp-j"> <li class="alt"><span class="keyword">Public <span class="keyword">boolean dispatchtouchevent (motionevent ev);</span></span></li> <li><span class="keyword">Public <span class="keyword">boolean onintercepttouchevent (motionevent ev); </span></span></li> <li class="alt"><span class="keyword">Public <span class="keyword">boolean ontouchevent (motionevent ev); </span></span></li> </ol> </ol><p><p></p></p><p><p></p></p><p><p><em>In activity, there are two callback functions:<br></em></p></p><p><p></p></p><p><p></p></p><strong><strong>[java]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_3" width="16" height="16" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_3" width="+" height="" align="middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "><param name=" pluginspage " value="http://www.macromedia.com/go/getflashplayer"><param name="loop" value="loop"> <param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always">< param name= "allowfullscreen" value= "false" ><param name="flashvars" value="id=3&width=16&height=16">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-j"> <ol class="dp-j"> <li class="alt"><span class="keyword">Public <span class="keyword">boolean dispatchtouchevent (motionevent ev);</span></span></li> <li><span class="keyword">Public <span class="keyword">boolean ontouchevent (motionevent ev); </span></span></li> </ol> </ol><p><p><em><br><br></em></p></p><p><p></p></p><p><p>By default in android, event delivery is received by the final view, and the delivery process is from the parent layout to the child layout, that is, from activity to ViewGroup to view, by default, ViewGroup is the pass-through Function. The event delivery process in Android (in the direction of arrows), as shown in [qiushuiqifei], thanks to [qiushuiqifei] finishing.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Touch events are a series of Action_down,action_move. MOVE ... MOVE, last action_up, Touch events, and Action_cancel Events. Events begin with action_down, and the Activity's dispatchtouchevent () first receives action_down, executes <span class="s1">super.dispatchtouchevent (ev), and distributes the events Down.<br></span></p></p><p><p>Dispatchtouchevent () returns true, subsequent events (action_move, Action_up) are passed again, and if the return false,dispatchtouchevent () is not received action_up, the ACTION _move.</p></p><p><p></p></p><p><p></p></p><p><p>The following images are referenced from [eoe]</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Figure 1. Action_down was not consumed.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Figure 2-1. Action_down was consumed by the view</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Figure 2-2. Subsequent action_move and up will look for the view without being Intercepted.</p></p><p><p></p></p><p><p></p></p><p><p>Figure 3. The follow-up was Intercepted.</p></p><p><p></p></p><p><p></p></p><p><p>Figure 4action_down was intercepted from the start</p></p><p><p></p></p><p><p></p></p><p><p>The touch events in Android start with Action_down:</p></p><p><p></p></p><p><p>Single Finger operation: action_down---action_move----action_up</p></p><p><p>Multi-finger operation: Action_down---action_pointer_down---action_move--action_pointer_up---action_up.</p></p><p><p></p></p><p><p><strong>"turn" http://blog.csdn.net/stzy00/article/details/40378533</strong></p></p><p><p>In the layout file, assume that there are 3 layers, one layer is button, TextView and other common components, the second layer is nested relativelayout, three layer is linearlayout, and a touch screen event is nothing but action_down, action_  MOVE, Action_up. And the finger from the press to release off the screen, in fact, the transmission of the event distribution has passed through the three Layers. Here the process of its processing, is also a consolidation of memory.</p></p><p><p></p></p><p><p>First of all, the three methods of Dispatchtouchevent (), onintercepttouchevent () and Ontouchevent () are available in each view, regardless of button,textview or linearlayout , relativelayout they all eventually inherit from view, viewgroup, so these three methods have Each. Dispatchtouchevent () is responsible for the distribution of touch screen events (down-move-up), Onintercepttouchevent () is responsible for interception in the event, and Ontouchevent () is the processing of the Event.</p></p><p><p>next, say how each of the three methods in the three layers participates in this process:</p></p><p><p></p></p><p><p>From the activity, received the finger press the screen of the event, namely action_down, the first to reach linearlayout, linearlayout call dispatchtouchevent (), dispatchtouchevent () is generally not used for rewriting, it is automatically distributed to Onintercepttouchevent (), if Onintercepttouchevent () action_down inside return fasle means that they do not handle the event, but to the next to deal with it , and then continue to distribute this event to the next layer of relativelayout, in the same relativelayout if Onintercepttouchevent () Action_down still return false, then arrive TextView here . When TextView returns true in Onintercepttouchevent ()), the event is handled by itself and is equivalent to intercepting the Event. when finished, returns true to reverse the processing of the event into the Activity. Tell the activity to handle it himself. The next two events, Action_move and action_up, will then continue to be uploaded to TextView and Processed.</p></p><p><p>If the first action_down is disposed of in Relativelayout (that is, the onintercepttouchevent () in Relativelayout returns True and is disposed of by its own ontouchevent (), The TextView on the lower level cannot participate in the transmission of this touch event, and the latter action_move and action_up, TextView will not be delivered). so if you want to intercept a touch action, Just do it in onintercepttouchevent (hehe, so good to understand some.) as long as the Action_down is intercepted, the move and up events are not passed down, and only the move event is intercepted, the up event is no longer passed DOWN. however, regardless of which view handles this touch event, the result of the final processing must be submitted to the activity in reverse, so that the user touches the screen to get the corresponding FEEDBACK.</p></p><p><p> Dispatchtouchevent in android, onintercepttouchevent, ontouchevent understanding </p> </p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.