Android Touch and gesture Operation Gesturedetector_android

Source: Internet
Author: User
Tags static class

Now the smartphone dare not say hundred are touch screen, it should be more than 99% for touch screen, touch screen for us to operate without a keyboard, no mouse mobile phone system has brought a lot of convenience. When the user touches the screen, it produces a lot of touch events, down, up, move, and so on. The view class has an View.ontouchlistener internal interface, and by rewriting his ontouch (View V, motionevent event) method, we can handle some touch events, as follows:

 public class Mainactivity extends activity {...//This example shows the activity, but
You are would use the same approach if//You were subclassing a View.
  
 @Override public boolean ontouchevent (Motionevent event) {int action = motioneventcompat.getactionmasked (event);
   Switch (action) {case (Motionevent.action_down): Log.d (Debug_tag, "action is Down");
  return true;
   Case (Motionevent.action_move): Log.d (Debug_tag, "ACTION is Move");
  return true;
   Case (MOTIONEVENT.ACTION_UP): Log.d (Debug_tag, "ACTION is up");
  return true;
   Case (Motionevent.action_cancel): Log.d (Debug_tag, "ACTION is CANCEL");
  return true; Case (Motionevent.action_outside): Log.d (Debug_tag, "Movement occurred OUTSIDE bounds" + "of the current screen Eleme
   NT ");  
  return true;
 Default:return super.ontouchevent (event); }  
}

The events provided by Ontouch are relatively simple, and using this interface can be cumbersome if you need to handle some complex gestures, because we have to judge what gestures are based on the user's touch path. The Android SDK provides us with the gesturedetector (gesture: Gesture Detector: Recognition) class, through which we can identify a lot of gestures.

public class Gesturedetector extends Object 
java.lang.Object
android.view.GestureDetector

Gesturedetector belongs to the Android.view package, and Android also offers android.gesture packs to support more gesture operations, which we'll introduce later. The official introduction used Gesturedetectorcompat processing gesture recognition, why the use of Gesturedetectorcompat replaced the Gesturedetector, the official explanation is this:

There are two methods of Gesturedetectorcompat instantiation:

The Gesturedetector class provides two external interfaces: Ongesturelistener,ondoubletaplistener, and an internal class Simpleongesturelistener The Simpleongesturelistener class is a more convenient class that Gesturedetector gives us to respond to different gestures, which implements the two interfaces, which are static class, which means that it is actually an external class, We can inherit this class from the outside and rewrite the gesture processing method inside. So there are two methods to realize gesture recognition, one is to implement Ongesturelistener interface, the other is to use Simpleongesturelistener class.

Ongesturelistener has the following several actions:

Press (Ondown): the moment when the finger touches the touch screen, is the touch of that.

Toss (onfling): the finger moves quickly on the touch screen and loosens.

Long Press (onlongpress): the finger presses for a period of time and does not loosen.

scrolling (onscroll): the finger slides on the touch screen.

Press and hold (onshowpress): the finger presses on the touch screen, its time range is pressed to work, before the long press.

lift (onsingletapup): the moment the finger leaves the touch screen.

Use the Ongesturelistener interface, which requires all the methods of overloading the Ongesturelistener interface to listen for all gestures, as stated in the official document "Detecing all supported gestures".

public class Mainactivity extends activity implements Gesturedetector.ongesturelistener, Gesturedetector.ondoubletap
 listener{private static final String Debug_tag = "gestures"; 

 Private Gesturedetectorcompat Mdetector; 
 Called the activity is the created.
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main); Instantiate the gesture detector with the//application context and a implementation of//Gesturedetector.ongest
  Urelistener mdetector = new Gesturedetectorcompat (this,this);
  Set the gesture detector as the double tap//listener.
 Mdetector.setondoubletaplistener (this);
  @Override public boolean ontouchevent (Motionevent event) {this.mDetector.onTouchEvent (event);
 Be sure to call the superclass implementation return Super.ontouchevent (event); @Override public boolean Ondown (Motionevent event) {LOG.D (Debug_tag, "Ondown:" + evenT.tostring ());
 return true; @Override public boolean onfling (motionevent event1, motionevent event2, float Velocityx, float velocityy) {Lo
  G.D (Debug_tag, "onfling:" + event1.tostring () +event2.tostring ());
 return true; 
 @Override public void Onlongpress (Motionevent event) {LOG.D (Debug_tag, "onlongpress:" + event.tostring ()); @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {log.d (Debu
  G_tag, "onscroll:" + e1.tostring () +e2.tostring ());
 return true;
 @Override public void Onshowpress (Motionevent event) {LOG.D (Debug_tag, "onshowpress:" + event.tostring ()); 
  @Override public boolean Onsingletapup (Motionevent event) {LOG.D (Debug_tag, "onsingletapup:" + event.tostring ());
 return true;
  @Override public boolean Ondoubletap (Motionevent event) {LOG.D (Debug_tag, "Ondoubletap:" + event.tostring ());
 return true; @Override public boolean ondoubletapevent (Motionevent event) {LOG.D (Debug_tag, "ondoubletapevent:" + event.tostring ());
 return true; @Override public boolean onsingletapconfirmed (Motionevent event) {LOG.D (Debug_tag, "onsingletapconfirmed:" + even
  T.tostring ());
 return true;
 }
}

  This can cause some gesture actions that we can't use, but also overload. The appearance of the Simpleongesturelistener class solves this problem for us, and if you want to "detecting a subset of supported gestures", Simpleongesturelistener is the best option.

 public class Mainactivity extends activity {private Gesturedetectorcompat Mdetector 

 ;
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
 Mdetector = new Gesturedetectorcompat (This, new Mygesturelistener ());
  @Override public boolean ontouchevent (Motionevent event) {this.mDetector.onTouchEvent (event);
 Return Super.ontouchevent (event); Class Mygesturelistener extends Gesturedetector.simpleongesturelistener {private static final String Debug_tag = " 
  
  Gestures "; 
   @Override public boolean Ondown (Motionevent event) {LOG.D (Debug_tag, "Ondown:" + event.tostring ());
  return true;
   @Override public boolean onfling (motionevent event1, motionevent event2, float Velocityx, float velocityy) {
   LOG.D (Debug_tag, "onfling:" + event1.tostring () +event2.tostring ());
  return true; }
 }
}

Finally, we also explain two questions:

1, Ontouchevent Why the use of Motioneventcompat, but not directly using motionevent. Because Motioneventcompat allows more action to fit into API 4.

2, the Android view how to use gestures, the following methods:

View MyView = Findviewbyid (R.id.my_view); 
  Myview.setontouchlistener (New Ontouchlistener () {public
   Boolean Ontouch (View V, motionevent event) {
    //... Respond to touch Events  
   this.mDetector.onTouchEvent (event);
    Return Super.ontouchevent (event);
   }
  );

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.