Reference: http://blog.jrj.com.cn/4586793646,5298605a.html thanks to this brother!
Android.view.VelocityTracker The main use is to track the rate of touch screen events (flinging events and other gestures gesture events) and to do some effects for up after.
1, Use the obtain () function to obtain an instance of the class.
2, some of the commonly used methods:
2.1, using the Addmovement (Motionevent event) function to pass the current move event to the Velocitytracker object, the parameter is the event object.
2.2, use the computecurrentvelocity (int units) function to calculate the current speed parameter units: The rate you use for Unit 1 means that the rate of how many pixels are moving in a millisecond, 1000 for one second
How many pixels are moving in time. The unit is in milliseconds.
2.3,computecurrentvelocity(int units, float maxvelocity) parameter 1 ibid., Parameter maxvelocity: This method can calculate the maximum rate of events. If the calculated rate is greater than maxvelocity
The resulting rate is maxvelocity, less than maxvelocity is the true rate of computation.
Example: Currentvelocity =currentvelocity>maxvelocity?maxvelocity:currentvelocity;
2.4, using getxvelocity (), getyvelocity () function to get the current horizontal and vertical speed
3, Example Demo:
1 PrivateVelocitytracker Mvelocitytracker;//Life Variables2 3 //In the ontouchevent (motionevent ev)4 5 6 if(Mvelocitytracker = =NULL) { 7Mvelocitytracker = Velocitytracker.obtain ();//get Velocitytracker class instances8 } 9Mvelocitytracker.addmovement (EV);//adding events to an instance of the Velocitytracker classTen One A //determine when EV events are motionevent.action_up: Calculation rate - FinalVelocitytracker Velocitytracker =Mvelocitytracker; - //provides pixels per second theVelocitytracker.computecurrentvelocity (1, (float) 0.01);//When setting the Maxvelocity value to 0.1, the rate is greater than 0.01, the display rate is 0.01, the rate is less than 0.01, the display is normal -LOG.I ("Test", "Velocitytraker" +velocitytracker.getxvelocity ()); - -Velocitytracker.computecurrentvelocity (1000);//set the value of units to 1000, which means how many pixels are moving in a second time +LOG.I ("Test", "Velocitytraker" +velocitytracker.getxvelocity ());