Android Velocitytracker Speed Monitor

Source: Internet
Author: User

Reprint http://blog.csdn.net/wobilini/article/details/9071579

Reprint http://blog.csdn.net/hudashi/article/details/7352157

Android.view.VelocityTracker is primarily used to track the rate of touch screen events (flinging events and other gestures gesture events). Use the addmovement (motionevent) function to add motion event to the Velocitytracker class instance. You can use Getxvelocity () or getxvelocity () to get horizontal and vertical rates to a rate , but before using them, call computecurrentvelocity (int) to initialize the units of the rate. Main functions
Public Methods
void Addmovement (Motionevent event) Add A user ' s movement to the tracker.
void Clear () Reset the velocity tracker the initial state.
void computecurrentvelocity (int units, float maxvelocity) Compute The current velocity based on the points that has been Collec Ted.int Unitis represents the base time unit of the rate. The Unitis value of 1 is how many pixels are moving within a millisecond unit of time, and a Unitis value of 1000 indicates the maximum number of pixels that are moving within a second (1000 milliseconds) unit of time floatvelocity the rate
void Computecurrentvelocity (int units) equivalent to invoking with computeCurrentVelocity(int, float) a maximum velocity of float.max_value.
Abstract T Getnextpoolable ()
Float Getxvelocity () Retrieve the last computed X velocity.
Float getxvelocity (int id) Retrieve The last computed X velocity.
Float getyvelocity (int id) Retrieve The last computed Y velocity.
Float Getyvelocity () Retrieve the last computed Y velocity.
Abstract Boolean Ispooled ()
Static Velocitytracker Obtain () Retrieve a new Velocitytracker object to watch the velocity of a motion.
void Recycle () Return a Velocitytracker object back to being re-used by others.
abstract void Setnextpoolable (T Element)
abstract void Setpooled (Boolean ispooled)
Example:     Private velocitytracker mvelocitytracker;//Life variables     //In Ontouchevent ( Motionevent ev)      if (Mvelocitytracker = = null) {             MV Elocitytracker = Velocitytracker.obtain ();//Get Velocitytracker class instances     }      Mvelocitytracker.addmovement (EV);//Add events to the Velocitytracker class instance      // Determine when EV events are motionevent.action_up: Calculation rate      Final velocitytracker velocitytracker = Mvelocitytracker;      //provides pixels per second      Velocitytracker.computecurrentvelocity (1, (float) 0.01),  //setting maxvelocity value of 0.1, the rate is greater than 0.01, the rate of display is 0.01, When the rate is less than 0.01, the normal      log.i ("Test", "Velocitytraker" +velocitytracker.getxvelocity ()) is displayed;                         VELOCITYTRACKER.COMPUTECURRENTVE Locity (+);  //sets the value of units to 1000, which means how many pixels are moving in a second time      LOG.I ("Test", "Velocitytraker" +velocitytracker.getxvelocity ());  The general use is this: when you need to track the speed of touch screen events, Use the obtain () method to get an instance object of the Velocitytracker class in the ontouchevent callback function, using Addmovement (motionevent) The function passes the current move event to the Velocitytracker object using the computecurrentvelocity   (int units) function to calculate the current speed, using  getxvelocity   (),  getyvelocity   () function to get the current speed      

Layout file Activity_main:

Here we only define a textview to show the speed at which our fingers move, and here we initialize it: Hello World

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:id= "@+id/tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Hello World"/>


</RelativeLayout>

Again down is mainactivity:

public class Mainactivity extends activity{

Private TextView TV;

Private Velocitytracker Vt=null;

protect void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

tv= () Findviewbyid (r.id.tv);

}

public boolean ontouchevent (Motionevent event) {

int action=event.getavtion ();

Switch (action) {

Case:MotionEvent.ACTION_DOWN:

if (vt==null) {

Initializes the Velocitytracker object VT to monitor the action of the motionevent.

Vt=velocitytracker.obtain ();

}else{

Vt.clear ();

}

Vt.addmovement (event);

Break

Case:MotionEvent.ACTION_MOVE:

Vt.addmovement (event);

Represents the measurement of the distance (in pixels) of a finger movement every 1000 milliseconds, which is the unit used to control VT, which is 1 in parentheses, which represents the number of m/px that a 1-millisecond finger has moved (ms/px

Vt.computecurrentvelocity (1000);

Here x is a regular for the finger to the right, the negative is the finger to the left, Y is special, the regular means the finger is sliding downward, and the negative means the finger swipe up

Tv.settext ("The x Velocity is" +vt.getxvelocity);

Tv.append ("The Y Velocity is" +vt.getyvelocity);

Break

Case:MotionEvent.ACTION_UP:

Case:MotionEvent.CANCEL:

Vt.recycle ();

Break

}

return true;

}

}

Android Velocitytracker Speed Monitor

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.