Android gesture Monitor

Source: Internet
Author: User
Tags gety

 Public classMainactivityextendsActivity {/** To implement the event that the finger slides left and right on the screen, you need to instantiate the object Gesturedetector,new * Gesturedetector (Mainactivity.this *, Ongesturelistener); first Implement Listener Object Gesturedetector *. Ongesturelistener, depending on the coordinates of the x or Y axis to determine whether to swipe left or right and to make event handling according to different gestures Doresult (int * action), * * Then overwrite ontouchevent method, onto     The Uchevent method passes the event object to Gesturedetector.ontouchevent (* event); */    PrivateGesturedetector Gesturedetector; Final intTOP = 1, BOTTOM = 2, left = 3, right = 4; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); System.out.println (Implementation); Gesturedetector=NewGesturedetector ( This, MyListener); }    PrivateGesturedetector.ongesturelistener MyListener =NewGesturedetector.simpleongesturelistener () { Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {            floatXexcursion = E1.getx ()-E2.getx (); floatYexcursion = E1.gety ()-e2.gety (); if(yexcursion > 0 && math.abs (yexcursion) >Math.Abs (xexcursion))            {executeAction (TOP); } Else if(Yexcursion < 0 && math.abs (yexcursion) >Math.Abs (xexcursion))            {executeAction (BOTTOM); } Else if(xexcursion > 0 && math.abs (xexcursion) >Math.Abs (yexcursion))            {executeAction (left); } Else{executeAction (right); }            return true;    }    };  Public Booleanontouchevent (Motionevent event) {returngesturedetector.ontouchevent (event); }     Public voidExecuteAction (intaction) {        Switch(action) { Case1: Toast.maketext ( This, "Up", Toast.length_short). Show ();  Break;  Case2: Toast.maketext ( This, "Down", Toast.length_short). Show ();  Break;  Case3: Toast.maketext ( This, "left", Toast.length_short). Show ();  Break;  Case4: Toast.maketext ( This, "right", Toast.length_short). Show ();  Break; default:             Break; }    }}

Android gesture Monitor

Related Article

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.