Use of Android Gesture listener class Gesturedetector

Source: Internet
Author: User
Tags gety

In the use of custom views, the handling of touch-screen events is essential, the ability to write their own code processing, so more flexible. If you don't want to be so troublesome, Android provides a gesture listener class gesturedetector that we can use. Gesturedetector is easy to use, provides a click, double click, long press and other operations, but the general definition of the interface is more complex, but also with a lot of attention to the place, here to summarize the use of gesturedetector.

First create a blank project, the main interface layout only need to add a button on the line

1<RelativelayoutXmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4Android:layout_height= "Match_parent"5Android:paddingbottom= "@dimen/activity_vertical_margin"6Android:paddingleft= "@dimen/activity_horizontal_margin"7Android:paddingright= "@dimen/activity_horizontal_margin"8Android:paddingtop= "@dimen/activity_vertical_margin"9Tools:context=". Mainactivity ">10 11 <button12 android:id=" @+id/btn_textgesture "13  Android:layout_ Width= "fill_parent" 14  Android:layout_height= "fill_parent" 15  Android:text=" @string/app_name "/>< Span style= "COLOR: #008080" >16 17 </relativelayout>        

Due to the touch screen event to be tested, all this button is larger, the main interface is the following effect:

First, introduce the basic idea of touch screen event processing. Touch screen generally has three basic events, down press, move moves, up left, through the three basic events monitoring, to determine what the user performed the operation. A standard touch screen operation is generally a combination of a series of basic events, in the Android framework, through the Ontouch () function to obtain basic touch-screen events, and functions like the onclick, is already a series of basic events combination.

For example, a down event occurred, no move event occurred prior to the up event, or the range of the move was small, and the down and up events were short intervals, which is a click or Singeltap event,

In contrast to the event of the physical keyboard key, the physical keyboard is operated after the down event, and the touch screen event is typically performed after the up event has occurred.

Here's the code for the activity.

1PackageCom.example.testgesture;23Importandroid.app.Activity;4ImportAndroid.os.Bundle;5ImportAndroid.util.Log;6ImportAndroid.view.GestureDetector;7ImportAndroid.view.GestureDetector.SimpleOnGestureListener;8ImportAndroid.view.MotionEvent;9ImportAndroid.view.View;10ImportAndroid.view.View.OnTouchListener;11ImportAndroid.widget.Button;1213PublicClass MainactivityExtendsActivity {1415PrivateButton Mbutton;16PrivateGesturedetector Mgesturedetector;1718@Override19ProtectedvoidOnCreate (Bundle savedinstancestate) {20Super. OnCreate (Savedinstancestate);21stSetcontentview (R.layout.activity_main);22Mgesturedetector =New Gesturedetector (ThisNewMyongesturelistener ());24Mbutton =(Button) Findviewbyid (r.id.btn_textgesture);Mbutton.setontouchlistener (NewOntouchlistener () {2728@Override29PublicBooleanOnTouch (View V, motionevent event) {LOG.I (GetClass (). GetName (), "onTouch-----" +Getactionname (Event.getaction ()));31Mgesturedetector.ontouchevent (event);32//Be sure to return true, otherwise you will not get the full event33ReturnTrue;34}35});36}3738Private String Getactionname (IntAction) {The String name = "";40Switch(action) {41CaseMotionevent.action_down: {The name = "Action_down";43Break;44}45CaseMotionevent.action_move: {* Name = "Action_move";47Break;48}49CaseMOTIONEVENT.ACTION_UP: {Name = "Action_up";51Break;52}53Default:54Break;55}56ReturnName57}5859Class MyongesturelistenerExtendsSimpleongesturelistener {60@Override61PublicBooleanOnsingletapup (Motionevent e) {LOG.I (GetClass (). GetName (), "onsingletapup-----" +Getactionname (E.getaction ()));63ReturnFalse;64}6566@Override67PublicvoidOnlongpress (Motionevent e) {LOG.I (GetClass () getName (), "onlongpress-----" +Getactionname (E.getaction ()));69}7071@Override72PublicBoolean onscroll (Motionevent E1, motionevent E2,Float Distancex,FloatDistancey) {73LOG.I (GetClass (). GetName (),"Onscroll-----" + getactionname (e2.getaction ()) + ", (" + e1.getx () + "," + e1.gety () + "), ("+ e2.getx () + "," + e2.gety () + ")");76ReturnFalse;77}78 @Override-Public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {Bayi log.i (getcl (). GetName (), "onfling-----" + getactionname (e2.getaction ()) + ", (" + e1.getx () + "," + e1.gety () + ")," ("" the "+ E2.G EtX () + "," + e2.gety () + ")"); return false; Onshowpress (motionevent e) {log.i (GetClass (). GetName (), "onshowpress-----" + Tionname (E.getaction ())); The @Override of the Ondown (Motionevent e) {94 log.i (GetClass (). GetName (), "ondown-----" + Getactionnam E (E.getaction ())); return false; 98 @Override The public boolean Ondoubletap (Motionevent e) {log.i (GetClass (). GetName (), "ondoubletap-----" + ge Tactionname (E.getaction ())); 101 return false;102}103 104 @Override105 Public boolean ondoubletapevent (Motionevent e) { 106 log.i (GetClass () getName (), "ondoubletapevent-----" + getactionname (E.getaction ())); 107 return false;108}109 110 @ Override111 public Boolean OnsingletapconFirmed (Motionevent e) {log.i (GetClass (). GetName (), "onsingletapconfirmed-----" + getactionname (E.getaction ())); 113 return false;114}115}116}

The first is to declare a gesturedetector and then rewrite the button's Ontouch function to give the touchscreen event to Gesturedetector.

First do a click on the button to make a

Onsingletapup is called, stating that a Click event occurred, Onsingletapconfirmed was called, stating that a Click event occurred, not a double-click event. It is important to note that Onsingletapup is already a click event and the Onsingletapup trigger is the action_up event. Onsingletapconfirmed is triggered when the user's finger leaves the screen , and all up is not the end of all touchscreen events.

Do a double-click operation

First a onsingletapup occurs, stating that a single click event has been completed, and then Ondoubletap occurs, a double-click event is completed. As we can see, ondoubletap occurs when the Action_down event, which means that the double-click event is triggered when the second time the screen is pressed, not the second time it leaves the screen, after the ondoubletap occurs, You can hear all the touch events from the press to bounce in the ondoubletapevent when the double-click event occurs. Onsingletapup and onsingletapconfirmed are not triggered after ondoubletap occurs.

Do a long-press operation

Onlongpress really action_down occurs, onlongpress occurs after the up will not be triggered by other events, you can change the status of the onshowpress processing, such as the button down state.

Do a swipe operation

Onscroll event is drag, Onfling is thrown. Combine log to get a look. First is the Action_down, after several action_move, moved more than a certain distance, triggered the onscroll, if the onscroll is triggered, there is no long press before the up, click, double-clicking Events . Take a look at the parameters of Onscroll

float Distancey)

E1 for the first press event, as in the Ondown event, E2 for the current event, Distancex for the onscroll movement of the x-axis distance, Distancey for the movement of the y-axis distance, moving distance is relative to the last onscroll event of the moving distance, Rather than the distance between the current point and the pressed point.

This slide finally triggered the Onfling event, but the Onfling event trigger is not certain, onfling is triggered in action_up, the usual list in the left screen is to continue scrolling, is triggered by this way.

The first two parameters of the onfling are the same as the onscroll, E2 the point when the user leaves the screen after dragging. Veloctiyx,velocity for the initial speed when leaving the screen, at these two speeds for the initial speed of the uniform deceleration movement, is now drag the list and drag the picture of the various cache scrolling effect.

return value of the function

In addition to onlongpress, these functions all have a return value,

1 Mbutton.setontouchlistener (NewOntouchlistener () {2 3  @Override  4 public boolean OnTouch (View V, Motionevent event) { 5 log.i (GetClass (). GetName (), "OnTouch-----" + Getactionname (Event.getaction ()));  6  Mgesturedetector.ontouchevent (event);  7 // Be sure to return true or not get the full event  8 return true ;  9 }10});     


These return values are passed Mgesturedetector.ontouchevent (event); Passed to Ontouch.

Finally, summarize

Gesturedetector combined with Simpleongesturelistener can easily get to the click, double click, long Press and other events, but the handling of these events is not simple in the corresponding function to do some work, Complex custom views are still in Ontouch to judge the focus state of the control, and Gesturedetector is not omnipotent, if you want to handle the long press after the move, it will take a lot of effort, Think Gesturedetector in the long press after the occurrence is not in the onscroll, you can only through the Ontouch inside the action_move processing.

Use of Android Gesture listener class Gesturedetector

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.