Use of the Android-ontouchevent method

Source: Internet
Author: User

Mobile Screen Event processing method ontouchevent. The method is defined in the view class, and all of the view subclasses override this method, which the application can use to handle touch events on the phone screen. The signature of the method is as follows.

 Public boolean ontouchevent (Motionevent event)   

Parameter event: The event encapsulates the object for the mobile screen touch events wrapper class, which encapsulates all the information about the event, such as the location of the touch, the type of touch, and the time of the touch. The object is created when the user touches the screen of the phone.

Return value: The mechanism of the return value of the method is the same as the keyboard response event, which returns True when the event has been completely processed and does not want the other callback methods to be processed again, otherwise false.

This method does not deal with only one event as described previously, and in general the following three cases are all handled by the Ontouchevent method, except that the action values in three cases are different.

The screen is pressed: when the screen is pressed, the method is automatically called to handle the event, at which point the value of Motionevent.getaction () is Motionevent.action_down, and if the event that the screen is pressed is required to be processed in the application, simply re-call the callback method , then the action in the method can be judged.

The screen is lifted: The event that is triggered when the stylus leaves the screen, the event also requires the Ontouchevent method to capture, and then the action is judged in the method. When the value of Motionevent.getaction () is motionevent.action_up, it is the event that the screen is lifted.

Drag in the screen: This method also handles the event that the stylus slides on the screen, and also calls the Motionevent.getaction () method to determine whether the action value is motionevent.action_move and then processed.

Android Touch screen is different from traditional click Touch screen, there are some gestures (Gesture), such as Fling,scroll and so on. These gesture will greatly enhance the user experience.
Gesture Recognition (detector) in Android is implemented through the Gesturedetector.ongesturelistener interface.
First of all, the Android event processing mechanism is based on listener implementation, such as touch-screen related events, is through the ontouchlistener implementation;
Second, all view subclasses can add listener to a class of events by means of Setontouchlistener (), Setonkeylistener (), etc.
Thirdly, listener is generally provided in the form of interface, which contains one or more abstract methods, and we need to implement these methods to perform operations such as OnTouch (), OnKey (). In this way, the program can give the appropriate response through the callback function when a particular event is dispatch to the view.

1. Touch Screen Click Example

 Public classMygestureextendsActivityImplementsontouchlistener{ Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);  Setcontentview (R.layout.main); TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.setontouchlistener ( This); }  Public BooleanOnTouch (View V, motionevent event) {Toast.maketext ( This, "Touch Touch", Toast.length_short). Show (); return false; }}

We can get the type of touch event through the Motionevent getaction () method, including the Action_down (press touch screen), action_move (moving under the touch screen by Lidian weights), Action_ Up (Release touch screen) and action_cancel (not directly triggered by the user). With Getrawx (), Getrawy (), GetX () and gety () and other methods to obtain coordinates, we can implement functions such as dragging a button, dragging a scrollbar, and so on, with the help of different user actions. 2. How do we identify the user's gesture when we capture the touch operation? Here we need the help of the Gesturedetector.ongesturelistener interface, the code is as follows:

 Public classMygestureextendsActivityImplementsOntouchlistener, ongesturelistener{PrivateGesturedetector Mgesturedetector;  Publicmygesture () {Mgesturedetector=NewGesturedetector ( This); }     Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.setontouchlistener ( This); Tv.setfocusable (true); Tv.setclickable (true); Tv.setlongclickable (true); Mgesturedetector.setislongpressenabled (true); }    /** * in the Ontouch () method, we call Gesturedetector's Ontouchevent () method, * Give the captured motionevent to Gesturedetector * To analyze if there is a suitable callback function to handle user gestures*/     Public BooleanOnTouch (View V, motionevent event) {returnmgesturedetector.ontouchevent (event); }    //user touches touch screen, triggered by 1 motionevent action_down     Public BooleanOndown (motionevent arg0) {log.i ("Mygesture", "Ondown"); Toast.maketext ( This, "Ondown", Toast.length_short). Show (); return true; }    /** * User Touch touchscreen , not released or dragged, triggered by a 1 motionevent action_down * * Note and ondown () difference, emphasizing that there is no release or drag state*/     Public voidonshowpress (motionevent e) {log.i ("Mygesture", "onshowpress"); Toast.maketext ( This, "Onshowpress", Toast.length_short). Show (); }    //user (after touch touchscreen) released, triggered by a 1 motionevent action_up     Public BooleanOnsingletapup (motionevent e) {log.i ("Mygesture", "Onsingletapup"); Toast.maketext ( This, "Onsingletapup", Toast.length_short). Show (); return true; }    //The user presses the touchscreen, moves quickly, and releases, triggered by 1 motionevent action_down, multiple action_move, and 1 action_up     Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {LOG.I ("Mygesture", "onfling"); Toast.maketext ( This, "Onfling", Toast.length_long). Show (); return true; }    //The user presses the touchscreen, and drags, by 1 motionevent action_down, multiple Action_move trigger     Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) {LOG.I ("Mygesture", "Onscroll"); Toast.maketext ( This, "Onscroll", Toast.length_long). Show (); return true; }    //User long press touch screen, triggered by multiple motionevent Action_down     Public voidonlongpress (motionevent e) {log.i ("Mygesture", "onlongpress"); Toast.maketext ( This, "Onlongpress", Toast.length_long). Show (); }}

3. Handling code for the Fling event: In addition to information such as the first fling action_down and the coordinates contained in the last Action_move, we can also be conditional on the movement speed of the user on the x-axis or the y-axis. For example, in the code below, we will only process when the user moves more than 100 pixels, and the x-axis moves faster than 200 pixels per second.

 Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {        //parameter explanation://E1:1th action_down motionevent//E2: The last Action_move motionevent//movement speed on velocityx:x axis, pixels per second//movement speed on velocityy:y axis, pixels per second//Trigger conditions://x-axis coordinate displacement greater than fling_min_distance, and moving faster than fling_min_velocity pixels per second        Final intFling_min_distance = +, fling_min_velocity = 200; if(E1.getx ()-E2.getx () >fling_min_distance&& Math.Abs (Velocityx) >fling_min_velocity) {            //Fling LeftLOG.I ("Mygesture", "Fling left"); Toast.maketext ( This, "Fling left", Toast.length_short). Show (); }        Else if(E2.getx ()-E1.getx () >fling_min_distance&& Math.Abs (Velocityx) >fling_min_velocity) {            //Fling RightLOG.I ("Mygesture", "Fling right"); Toast.maketext ( This, "Fling Right", Toast.length_short). Show (); }        return false; }

In this example, Tv.setlongclickable (true) is necessary, because only then can the view handle a hold (i.e. Action_move, or multiple Action_down) that differs from tap. We can also do this by android:longclickable in the layout definition.

This article transferred from: http://blog.csdn.net/tianfeng701/article/details/7556366

Use of the Android-ontouchevent method

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.