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