Use of Android extension-velocitytracker, Gesturedetector

Source: Internet
Author: User

Recently watching Ningyugang teacher's "Android development art exploration", combined with the knowledge of the teacher's books and online information to simply record their understanding of these

1.VelocityTracker class

The Velocitytracker class is a speed-tracking class for Android gestures that tracks the speed during sliding, including both horizontal and vertical speeds. Here is a brief introduction to how this is used.

This class is definitely used if we want to track the swipe speed of a gesture on a view. In fact, we can also do the calculation in other ways.

For example:

A. In the down event, record the current time, the XY coordinates of the event.

B. In the up event, to perform the calculation.

But this way has a disadvantage, one is to calculate is more troublesome, followed by if the user is sliding a back and forth, the relative distance is 0, then the speed of 0, certainly is wrong. The Velocitytracker class can simplify our operations. The usage of the Velocitytracker class is described below.

(1). Initialize the object of the Velocitytracker class

Normally we initialize the Velocitytracker class with the down event in the view's Ontouchevent method, The Velocitytracker object is obtained by calling the static method--obtain method of the Velocitytracker class.

1 // get the object of the Velocitytracker class 2  Velocitytracker = Velocitytracker.obtain ();

(2). Get speed

Usually we get the speed in the up event, but when we get the speed, we need to be aware that before we get the speed, we set a thing, that is, the unit time, by calling the Computecurrentvelocity (int units) method to set a time period.

Some people have doubts, what is the use of this? For example, we say that a speed is 100/1000ms, that is, 100/1s, if we call the above method to set a unit time, for example, set to 5000 (in ms), then the speed becomes 500/5000ms. This is what the Computecurrentvelocity method does!

After setting the unit time, we can call the Getxvelovity () method and the Getyvelocity () method directly to get the horizontal and vertical velocity.

1                 //Registering Events2 velocitytracker.addmovement (event);3                 //Set unit time4Velocitytracker.computecurrentvelocity (100);5                 //Horizontal Speed6                 floatXvelocity =velocitytracker.getxvelocity ();7                 //Vertical Speed8                 floatYvelocity =velocitytracker.getyvelocity ();9LOG.I ("main", "xvelocity =" + xvelocity + "yvelocity =" +yvelocity);Ten                  Break;

It is also important to note that before we can calculate the speed, we must first call the Addmoveevent method to register the event.

(3). Releasing the Velocitytracker object

After we run out of Velocitytracker objects, we must call the Clear method and the Recycle method to release the Velocitytracker object. Usually this operation is done in up and cancel, preferably at the same time!

1     Private void Releasevelocitytracker () {2         if NULL ) {3            velocitytracker.clear (); 4             velocitytracker.recycle (); 5         }6     }

(4). Complete code

1  Public classMyViewextendsView {2     PrivateVelocitytracker Velocitytracker =NULL;3 4      PublicMyView (Context context) {5         Super(context);6     }7 8      PublicMyView (Context context, @Nullable AttributeSet attrs) {9         Super(context, attrs);Ten     } One  A      PublicMyView (context context, @Nullable AttributeSet attrs,intdefstyleattr) { -         Super(context, attrs, defstyleattr); -     } the  - @Override -      Public Booleanontouchevent (Motionevent event) { -  +         Switch(Event.getaction ()) { -              CaseMotionevent.action_down: { +                 //get the object of the Velocitytracker class AVelocitytracker =Velocitytracker.obtain (); at                  Break; -             } -              CaseMotionevent.action_move: { -                 //Registering Events - velocitytracker.addmovement (event); -                 //Set unit time inVelocitytracker.computecurrentvelocity (100); -                 //Horizontal Speed to                 floatXvelocity =velocitytracker.getxvelocity (); +                 //Vertical Speed -                 floatYvelocity =velocitytracker.getyvelocity (); theLOG.I ("main", "xvelocity =" + xvelocity + "yvelocity =" +yvelocity); *                  Break; $             }Panax Notoginseng              Casemotionevent.action_up: { -                 //releasing the Velocitytracker object the Releasevelocitytracker (); +                  Break; A             } the              CaseMotionevent.action_cancel: { + Releasevelocitytracker (); -                  Break; $             } $         } -         return true; -     } the  -     Private voidReleasevelocitytracker () {Wuyi         if(Velocitytracker! =NULL) { the velocitytracker.clear (); - velocitytracker.recycle (); Wu         } -     } About}

2.GestureDetector class

A class of gesture detection that is used to assist in checking the behavior of a user's clicks, slips, long presses, double clicks, and so on. The use of the Gesturedetector class is not very troublesome!

(1). Create an object of the Gesturedetector class

It should be noted that the Gesturedetector construction method usually does not contain the structure of the context is out of date, while the construction method requires us to pass in a Ongesturelistener object, we can define an interface, Implement the Ongesturelistener interface, or you can use an anonymous inner class. The method in the interface is explained, and will be represented by a table later.

(2). Gesturedetector object takes over the Ontouchevent event of view

We just need to pass the event in the view's ontouchevent into the Ontouchevent method of the Gesturedetector object.

1     @Override 2      Public Boolean ontouchevent (Motionevent event) {3         // Gesturedetector Takeover Event 4         return gesturedetector.ontouchevent (event); 5     }

(3). Method Explanation

There are many abstract methods in the Ongesturelistener interface, where a table is used to show the function

Method name Explain
Ondown The finger touches the screen momentarily, triggered by a action_down
Onshowpress

Finger lightly touch the screen, not yet loose or dragged, triggered by a action_down

Note the difference from the Ondown method, which emphasizes that there is no release or drag state

Osingletapup The finger (after touching the screen) is released, triggered by a motionevent.action_up, which is the behavior of the Click event
Onscroll The finger presses the screen and drags, triggered by a action_down, multiple action_move, which is the drag behavior
Onlongpress The user presses the screen permanently, long press
Onfling

The user presses the touchscreen, quickly slips and releases, triggered by a action_down, multiple action_move, and a action_up,

This is fast sliding behavior

What if we want to implement a double-click event? In the Gesturedetector object, there is a method Setondoubletaplistener (Ondoubletaplistener Ondoubletaplistener) that can set the listener for the double-click event. Here again the method function of the Ondoubletaplistener interface is explained.

Method name Explain
Ondoubletap Double-click, consisting of 2 consecutive clicks, which cannot coexist with the Onsingletapconfirmed method
Onsingletapconfirmed

Strict click behavior

Notice the difference between it and the Onsingletapup, if the Onsingletapconfirmed method is triggered, then it is not possible to follow the back closely

Another click click Behavior, that is, you can only click, and cannot be double-struck

Ondoubletapevent Indicates that a double-click behavior has occurred, and Action_down,action_move and ACTION_UP will trigger this callback during double-click.

Use of Android extensions-velocitytracker, Gesturedetector

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.