Android Gesture Recognition simple encapsulation class

Source: Internet
Author: User
Tags abs

A simple gesture function is used to do a project (left, right, slide the screen), so it simply encapsulates the

Import Android.content.Context;

Import Android.util.DisplayMetrics;

Import Android.view.GestureDetector;

Import Android.view.GestureDetector.OnGestureListener;

Import android.view.MotionEvent;

public class Gesturehelper implements Ongesturelistener {

Private Gesturedetector Gesture_detector;

private int screen_width;

Private Onflinglistener listener_onfling;

public static abstract class Onflinglistener {

public abstract void Onflingleft ();

public abstract void Onflingright ();

}

Public Gesturehelper {

Displaymetrics DM = context.getresources (). Getdisplaymetrics ();

Screen_width = Dm.widthpixels;

Gesture_detector = new Gesturedetector (context, this);

}

public void Setonflinglistener (Onflinglistener listener) {

listener_onfling = listener;

}

public boolean ontouchevent (Motionevent event) {

Return Gesture_detector.ontouchevent (event);

}

@Override

public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {

Trigger Condition:

The x-coordinate of the x-axis is greater than the fling_min_distance and the moving speed is greater than fling_min_velocity pixels per second

Final int fling_min_distance = (int) (screen_width/3.0f), fling_min_velocity = 200;

if (E1.getx ()-E2.getx () > Fling_min_distance && math.abs (Velocityx) > Fling_min_velocity) {

Listener_onfling. Onflingleft ();

else if (E2.getx ()-E1.getx () > Fling_min_distance && math.abs (Velocityx) > Fling_min_velocity) {

Listener_onfling. Onflingright ();

}

return true;

}

@Override

public boolean Ondown (Motionevent e) {

return false;

}

@Override

public void Onlongpress (Motionevent e) {

}

@Override

public boolean onscroll (Motionevent E1, motionevent E2, float Distancex,

Float Distancey) {

return false;

}

@Override

public void Onshowpress (Motionevent e) {

}

@Override

public boolean onsingletapup (Motionevent e) {

return false;

}

}

Use the following methods:

public class Testactivity extends activity {

Private Gesturehelper GH;

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_start);

GH = new Gesturehelper (this);

Gh.setonflinglistener (New Onflinglistener () {

@Override

public void Onflingleft () {

Slide Left

}

@Override

public void Onflingright () {

Slide Right

}

});

}

@Override

public boolean ontouchevent (Motionevent event) {

Return Gh.ontouchevent (event);

}

}

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.