Android monitor finger swipe up or down screen

Source: Internet
Author: User
Tags gety

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

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.