"Android Notes" Velocitytracker use example

Source: Internet
Author: User

Velocitytracker can get the rate of touch-screen events. steps to use:

1. Get the Velocitytracker instance:

Velocitytracker Mvelocitytracker = Velocitytracker.obtain ();
2. Incorporate the Motionevent event into the Velocitytracker:
Mvelocitytracker.addmovement (event);
3. Calculate the instantaneous speed:
Mvelocitytracker.computecurrentvelocity (units,maxvelocity);
4. Get the speed in the XY direction:
float VX = mvelocitytracker.getxvelocity (Pointid), float VY = mvelocitytracker.getyvelocity (Pointid);
5. Recycling Velocitytracker:
Mvelocitytracker.clear (); mvelocitytracker.recycle ();
Examples of use:

Package Com.example.velocitytrackerdemo;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.view.motionevent;import Android.view.velocitytracker;import Android.view.viewconfiguration;public class Mainactivity extends activity{private static final String TAG = " Mainactivity ";p rivate velocitytracker mvelocitytracker;private int mmaxflingvelocity; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mmaxflingvelocity = Viewconfiguration.get (this). Getscaledmaximumflingvelocity ();} /** * Get Speed Tracker * @return */private velocitytracker getvelocitytracker () {if (Mvelocitytracker = = null) {Mvelocitytracker = Ve Locitytracker.obtain ();} return mvelocitytracker;} /** * Recovery speed tracker */private void Recyclevelocitytracker () {if (Mvelocitytracker! = null) {mvelocitytracker.clear (); Mvelocitytracker.recycle (); mvelocitytracker = null;}} @Overridepublic boolean ontouchevent (Motionevent event) {//1. Get speed Tracker GEtvelocitytracker ();//2. Include the current event in the tracker Mvelocitytracker.addmovement (event); int pointid = -1;switch (Event.getaction () {Case MOTIONEVENT.ACTION_DOWN:LOG.I (TAG, "Action_down");p Ointid = Event.getpointerid (0); break;case MOTIONEVENT.ACTION_MOVE:LOG.I (TAG, "Action_move");//3. Calculates the current speed mvelocitytracker.computecurrentvelocity (1000, mmaxflingvelocity);//gain x y direction speed float VX = mvelocitytracker.getxvelocity (pointid); float VY = Mvelocitytracker.getyvelocity (Pointid); LOG.I (Tag, "VX =" +vx+ ", VY =" +vy "); break;case motionevent.action_up:log.i (Tag," action_up ");//4. Recovery Speed tracker Recyclevelocitytracker (); break;default:break;} Return Super.ontouchevent (event);}}







"Android Notes" Velocitytracker use example

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.