Android-gestruedetector for Gesture Recognition

Source: Internet
Author: User

When using Android phones, you will often encounter a lot of applications that do a great job, such as sliding your fingers to the left or right of the screen to switch the interface. This actually uses a gesturedetector object gesture: gesture-making gesture, detector: monitoring the detector. In fact, it is to determine the next action by calculating the XY axis displacement from the screen to the screen, and the moving speed.

There are three steps to implement a sliding listener (click, double-click, long-press, and so on) for a widget in gesture recognition: 1. Create a defaultgesturedetector class that inherits from the simpleongesturelistener override onfling event 2. instantiate a gesture recognition object gesturedetector GD = new gestruedetector (new feature (); 3. Override the ontouchevent () event (activity override directly, the control is setontouchlistener ())

Step 1:

Class defaultgesturedetector extends simpleongesturelistener {
@ Override
Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy ){
Final int fling_min_distance = 100; // The distance between the X and Y axes (in pixels)
Final int fling_min_velocity = 200; // The movement speed on X or Y axis (pixel/second)
If (e1.getx ()-e2.getx ()> fling_min_distance & math. Abs (velocityx)> fling_min_velocity)
Toast. maketext (main. This, "Slide left", Toast. length_short). Show ();
Else if (e2.getx ()-e1.getx ()> fling_min_distance & math. Abs (velocityx)> fling_min_velocity)
Toast. maketext (main. This, "slide to the right", Toast. length_short). Show ();
Return false;

The above e1.getx ()-e2.getx () is the moving speed from the X axis to the velocityx: the X axis.

Part 2:

    private GestureDetector gestureDetector;
public void onCreate(Bundle savedInstanceState) {
gestureDetector=new GestureDetector(new DefaultGestureDetector());


Part III:

public boolean onTouchEvent(MotionEvent event){
return gestureDetector.onTouchEvent(event);
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.