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