When developing an Android program, sometimes you need to monitor your finger to slide the screen, and when your finger slides up and down in different directions to make a different response, how do you achieve that?
Using the gesture monitor provided by Android can be easily implemented, directly on the code (tested pass)
public class Cbmainactivity extends Activity implements android.view.gesturedetector.ongesturelistener{// Defines a gesture detector instance gesturedetector detector; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.cb_main); Create gesture Detector detector = new Gesturedetector (this,this); }//The touch event on the activity is given to Gesturedetector processing public boolean ontouchevent (motionevent me) {return detector.ontouchevent (me); } @Overridepublic Boolean Ondown (Motionevent arg0) {return false;} /** * * */@Overridepublic Boolean onfling (motionevent E1, motionevent E2, float velocityx,float velocityy) {float mi Nmove = 120; Minimum sliding distance float minvelocity = 0; Minimum sliding speed Float BeginX = E1.getx (); float EndX = E2.getx (); Float beginy = E1.gety (); float EndY = e2.gety (); if (Beginx-endx>minmove&&math.abs (Velocityx) >minvelocity) {//Zoli Toast.maketext ( this,velocityx+ "left Slide", Toast.length_short). Show ();} else if (Endx-beginx>minmove&&math.abs (Velocityx) >minvelocity) {//Right slide Toast.maketext (this,velocityX+ " Right slide ", Toast.length_short). Show ();} else if (Beginy-endy>minmove&&math.abs (velocityy) >minvelocity) {//Slide up Toast.maketext (this,velocityX+ " Slide ", Toast.length_short). Show (); else if (Endy-beginy>minmove&&math.abs (velocityy) >minvelocity) {//Slide Toast.maketext (this,velocityX+ " Slide ", Toast.length_short). Show ();} return false;} @Overridepublic void Onshowpress (Motionevent arg0) {//TODO auto-generated Method stub} @Overridepublic Boolean Onsingletapup (motionevent arg0) {//TODO auto-generated method Stubreturn false;} @Overridepublic void Onlongpress (Motionevent arg0) {//TODO auto-generated Method stub} @Overridepublic Boolean Onscroll ( Motionevent E1, motionevent E2, float velocityx,float velocityy) {return false;}}
Android monitor finger swipe up or down screen