Example of an Android gesture operation (up/down/left/right judgment) _android

Source: Internet
Author: User
Tags abs gety static class

The example of this article describes the Android gesture operation method. Share to everyone for your reference, specific as follows:

Android provides an interface for judging gestures, so we can implement a variety of gesture functions based on the API provided to improve the user experience of mobile apps.

Here is a small demo I wrote:

Gestureactivity.java

public class Gestureactivity extends activity {private Gesturedetector gesturedetector;
  private screen screens;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Gesturedetector = new Gesturedetector (This,ongesturelistener);
  Get the size of screen = Gestureutils.getscreenpix (this);
  @Override public boolean ontouchevent (Motionevent event) {return gesturedetector.ontouchevent (event);
    } gesturedetector.ongesturelistener Ongesturelistener = new Gesturedetector.simpleongesturelistener () {@Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {float x = E2.get
      X ()-E1.getx ();
      Float y = e2.gety ()-e1.gety ();
      The limit must be 1/3 across the screen to calculate the float x_limit = SCREEN.WIDTHPIXELS/3;
      float y_limit = SCREEN.HEIGHTPIXELS/3;
      float X_abs = Math.Abs (x);
      float y_abs = Math.Abs (y); if (x_abs >= y_abs) {//gesture left or right if (x > X_limit | | | x <-X_LIMIT) {if (x>0) {
          Right show (' right ');
          }else if (x//left Show ("left"); 
            }}else{//gesture down or up if (Y > Y_limit | | | y <-y_limit) {if (y>0) {
          Down Show ("Down");
          }else if (Y//up show ("Up");
    }} return true;
  }
  };
  private void Show (String value) {Toast.maketext (this, value, Toast.length_short). Show ();

 }
}

Gestureutils.java

public class Gestureutils {
  //Get screen size public
  static Screens Getscreenpix (context context) {
    Displaymetrics DM = new Displaymetrics ();
    WindowManager WindowManager = (windowmanager) context.getsystemservice (context.window_service);
    Windowmanager.getdefaultdisplay (). Getmetrics (DM);
    Return to New screen (dm.widthpixels,dm.heightpixels);
  }
  public static class screen{public
    int widthpixels;
    public int heightpixels;
    Public screens () {
    } public screen
    (int widthpixels,int heightpixels) {
      this.widthpixels=widthpixels;
      this.heightpixels=heightpixels;
    }
    @Override public
    String toString () {return
      "(" +widthpixels+ "," +heightpixels+ ")" "
    ;
  }
}}

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with your Android programming.

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.