---restore content starts---
1.Touch Event Delivery mechanism
The procedure is somewhat similar to the stack, and the subclass of ViewGroup has the following 3 methods that inherit it:
public boolean dispatchtouchevent (Motionevent event); Message distribution, which is equivalent to calling other functions in one function
public boolean onintercepttouchevent (Motionevent event); Intercept messages
public boolean ontouchevent (Motionevent event); Touch-screen processing, if the completion process returns true, equivalent to break quits the message processing directly, and returns false if no processing is completed, and returns to the Ontouchevent () of the view control that distributes the message to it to continue the recursive processing. Make a return to the message source
Message distribution is called tunneling, and touch-screen messages are passed to the upper control in the child view, called bubbling.
2. Distinguish between Ontouch () and Ontouchevent ()
OnTouch (): Defined in interface Ontouchlistener, after binding the touchscreen listener, override this method to implement custom touchscreen behavior
Ontouchevent (): In the Activity of the method, when the screen has a touch event call this method, if the screen has been pressed, it will always loop calls, My computer is about dozens of milliseconds to call once, but this no tube. Of course, the OnTouch () method will always be called when you keep pressing the bound control.
3.onTouchEvent () Processing of messages
The Ontouchevent () method is inherited from the activity, so it only needs to be covered in the activity, and it handles the following 3 kinds of messages
1) Press the screen: Motionevent.action_down
2) Release from the screen: motionevent.action_up
3) Move on the screen: Motionevent.action_move
1 @Override2 Public Booleanontouchevent (Motionevent event) {3 int[] Events = {4 Motionevent.action_down,5 Motionevent.action_move,6 Motionevent.action_up,7 Motionevent.action_cancel,8 Motionevent.action_outside,9 Motionevent.action_pointer_down,Ten Motionevent.action_pointer_up, One Motionevent.edge_top, A Motionevent.edge_bottom, - Motionevent.edge_left, - Motionevent.edge_right the }; -string[] Szevent = { -"Motionevent.action_down", -"Motionevent.action_move", +"Motionevent.action_up", -"Motionevent.action_cancel", +"Motionevent.action_outside", A"Motionevent.action_pointer_down", at"Motionevent.action_pointer_up", -"Motionevent.edge_top", -"Motionevent.edge_bottom", -"Motionevent.edge_left", -"Motionevent.edge_right" - }; in for(inti=0;i<events.length;++i) { - if(Events[i] = =event.getaction ()) { to log.v (Tag,szevent[i]); + Break; - } the } * return Super. Ontouchevent (event); $}
By overriding the method above, it is important to note that the move event is triggered when you click Release and triggers multiple times! So in the gesture recognition process part of the function parameter will only record the last move event
4. Gesture Recognition: Android.view.GestureDetector class + Ongesturelistener interface
There are a lot of methods, after the overwrite can achieve a variety of touch screen effect, increase the user experience
Using the Gesturedetector object, add a listener to the object and overwrite the method. This object can be used as a property of the activity, and of course the most activity is the gesture parsing. Then overwrite the activity's Ontouchevent method and listen to the touch screen event.
1 PrivateGesturedetector Gesturedetector =NewGesturedetector (NewGesturedetector.ongesturelistener () {2 @Override3 Public BooleanOndown (motionevent e) {4 //when I press the screen,5Toast.maketext (mainactivity. This, "Ondown", Toast.length_short). Show ();6LOG.V (TAG, "Ondown");7 return false;8 }9 Ten @Override One /** A * Clicked on the screen, but did not move and bounce action. The difference from Ondown: - * Ondown (): Once the screen is pressed, try the Ondown event - * onshowpress (): Ondown event has not been moved and bounced for a period of time (Ondown event was generated first) the * - */ - Public voidonshowpress (motionevent e) { -LOG.V (TAG, "onshowpress"); +Toast.maketext (mainactivity. This, "Onshowpress", Toast.length_short). Show (); - } + A @Override at /** - * Tap on the touch screen and bounce, if the Onlongpress,onscroll and onfling events are generated in this process, there will be no Onsingletabup event - */ - Public BooleanOnsingletapup (motionevent e) { -LOG.V (TAG, "Onsingletabup"); -Toast.maketext (mainactivity. This, "Onsingletagup", Toast.length_short). Show (); in return false; - } to + @Override - /** the * Scrolling events, when moving quickly on the screen, generate Onscroll, generated by Action_move * * @param: Distancex: Distance from the X-axis after the last occurrence of the onscroll event $ */Panax Notoginseng Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) { -LOG.V (TAG, "onscroll"); theToast.maketext (mainactivity. This, "Onscroll", Toast.length_short). Show (); + return false; A } the + @Override - Public voidonlongpress (motionevent e) { $LOG.V (TAG, "onlongpress"); $Toast.maketext (mainactivity. This, "Onlongpress", Toast.length_short). Show (); - } - the @Override - /**Wuyi * @param: E1: First Action_down motionevent the * @param: E2: Last Action_move motionevent - * @param: Movement speed on velocityx:x axis px/s Wu * @param: Movement speed on the velocityy:y axis - */ About Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) { $LOG.V (TAG, "onfling"); -Toast.maketext (mainactivity. This, "Onfling", Toast.length_short). Show (); - - return false; A } + }); the - @Override $ Public Booleanontouchevent (Motionevent event) { theLOG.V (TAG, "Ontouchevent"); the if(Gesturedetector.ontouchevent (event)) { the return true; the}Else{ - return Super.ontouchevent (event);//incomplete processing to upper control in }
the}
5. Handling Keyboard Events
Overwrite Activity onkeydown (), some need to add permissions in the Androidmanifest.xml file ...
1 Public BooleanOnKeyUp (intKeyCode, KeyEvent event) {2 Switch(keycode) {3 CaseKeyevent.keycode_home://as if to say no longer support, need to modify the framework source code implementation, a bit complex, looking forward to new discoveries4LOG.V (TAG, "HOME up");5 Break;6 CaseKeyevent.keycode_back:7LOG.V (TAG, "Back Up");8 Break;9 CaseKeyevent.keycode_dpad_left:TenLOG.V (TAG, "left up"); One Break; A } - //return true; - return Super. OnKeyUp (KeyCode, event); the}
Android Touch Event Summary