Velocitytracker
Primarily for touch event, Velocitytracker calculates the current speed in real time by tracking a sequence of events.
Method
//get a Velocitytracker object and remember to recycle it when you're done with it//After recycling means that you do not need to use, the system will assign this object to other requestersStatic PublicVelocitytracker obtain (); Public voidrecycle (); //calculates the current speed, where units is expressed in units, 1 for px/milliseconds, and 1000 for px/seconds. //maxvelocity The maximum value you want for this calculation speed Public voidComputecurrentvelocity (intUnitsfloatmaxvelocity); //after a computecurrentvelocity, you can use a few methods to get the value of this calculation.//The ID is the ID of the touch event point of contact, which is used to identify the multitouch , which can be ignored when calculating//other contact interference, of course, there must be interference. Public floatgetxvelocity (); Public floatgetyvelocity (); Public floatGetxvelocity (intID); Public floatGetyvelocity (intID);
Code
Public classVelocitytrackertestextendsActivity {PrivateTextView MInfo; PrivateVelocitytracker Mvelocitytracker; Private intmmaxvelocity; Private intMpointerid; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); MInfo=NewTextView ( This); Minfo.setlines (4); Minfo.setlayoutparams (Newlayoutparams (layoutparams.fill_parent, layoutparams.fill_parent)); Minfo.settextcolor (Color.White); Setcontentview (MInfo); Mmaxvelocity= Viewconfiguration.get ( This). Getmaximumflingvelocity (); } @Override Public Booleanontouchevent (Motionevent event) {if(NULL==Mvelocitytracker) {Mvelocitytracker=Velocitytracker.obtain (); } mvelocitytracker.addmovement (event); FinalVelocitytracker Vertracker =Mvelocitytracker; Switch(Event.getaction ()) { CaseMotionevent.action_down://the ID of the first contact, there may be multiple contacts at this point, but at least oneMpointerid = Event.getpointerid (0); Break; CaseMotionevent.action_move://seeking pseudo-instantaneous velocityVertracker.computecurrentvelocity (1000, mmaxvelocity); floatVelocityx =vertracker.getxvelocity (Mpointerid); floatVelocityy =vertracker.getyvelocity (Mpointerid); Recodeinfo (Velocityx, velocityy); Break; Casemotionevent.action_up: CaseMotionevent.action_cancel:if(NULL!=Mvelocitytracker) {mvelocitytracker.clear (); Mvelocitytracker.recycle (); Mvelocitytracker=NULL; } Break; default: Break; } return Super. Ontouchevent (event); } Private Static FinalString sformatstr = "Velocityx=%f\nvelocityy=%f"; /*** Record Current speed * *@paramVelocityx X-Axis speed *@paramvelocityy y-axis speed*/ Private voidRecodeinfo (Final floatVelocityx,Final floatvelocityy) { FinalString info =String.Format (Sformatstr, Velocityx, velocityy); Minfo.settext (info); } }I'm the dividing line of the king of the land Tiger.
Reference: http://blog.csdn.net/bingxianwu/article/details/7446799
Android--Velocitytracker