Velocitytracker, as the name implies, is speed tracking, which is used primarily in touch events (such as fling in gestures, scrolling, etc.) in Android, and Velocitytracker is calculated in real time by tracking a sequence Let's take a brief look at the usage.
For reference: Introduction to this article
//Get a Velocitytracker object and remember to recycle when you're done//After recycling means that you do not need to use the system to assign this object to other requestorsStatic PublicVelocitytrackerobtain(); Public void Recycle();//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 void computecurrentvelocity(intUnitsfloatmaxvelocity);//After a computecurrentvelocity you can use a few methods to get the value of this calculationThe //id is the ID of the touch event point of contact for multi-touch identification, which can be ignored when calculating .//Other contact interference, of course there must be interference. Public float getxvelocity(); Public float getyvelocity(); Public float getxvelocity(intID); Public float getyvelocity(intID);
PackageCom.github.c.horizonalscrollitem;Importandroid.app.Activity;ImportAndroid.graphics.Color;ImportAndroid.os.Bundle;ImportAndroid.view.MotionEvent;ImportAndroid.view.VelocityTracker;ImportAndroid.view.ViewConfiguration;ImportAndroid.view.ViewGroup.LayoutParams;ImportAndroid.widget.TextView; Public class velocitytrackertest extends Activity { PrivateTextView MInfo;PrivateVelocitytracker Mvelocitytracker;Private intmmaxvelocity;Private intMpointerid;@Override protected void onCreate(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);//0. Get this maximum rateMmaxvelocity = Viewconfiguration.get ( This). Getmaximumflingvelocity (); }@Override Public Boolean ontouchevent(Motionevent event) {Final intAction = Event.getaction (); Acquirevelocitytracker (event);//1. Adding motionevent to Velocitytracker FinalVelocitytracker vertracker = Mvelocitytracker;Switch(action) { CaseMotionevent.action_down://2. For 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://3. Seeking pseudo-instantaneous velocityVertracker.computecurrentvelocity ( +, mmaxvelocity);Final floatVelocityx = vertracker.getxvelocity (Mpointerid);Final floatVelocityy = vertracker.getyvelocity (Mpointerid); Recodeinfo (Velocityx, velocityy); Break; CaseMOTIONEVENT.ACTION_UP://4.ACTION_UP release velocitytracker, hand it over to other controlsReleasevelocitytracker (); Break;//5.action_cancel release velocitytracker, hand it over to other controls CaseMotionEvent.ACTION_CANCEL:releaseVelocityTracker (); Break;default: Break; }return Super. Ontouchevent (event); }/** * * @param event to velocitytracker add motionevent * * @see Android.view.Vel Ocitytracker#obtain () * @see android.view.velocitytracker#addmovement (motionevent) * * Private void Acquirevelocitytracker(FinalMotionevent event) {if(NULL= = Mvelocitytracker) {Mvelocitytracker = Velocitytracker.obtain (); } mvelocitytracker.addmovement (event); }/** * Release Velocitytracker * * @see android.view.velocitytracker#clear () * @see Android.view.velocitytracker#recycle () */ Private void Releasevelocitytracker() {if(NULL! = Mvelocitytracker) {mvelocitytracker.clear (); Mvelocitytracker.recycle (); Mvelocitytracker =NULL; } }Private Static FinalString Sformatstr ="Velocityx=%f\nvelocityy=%f";/** * Record Current speed * * @param Velocityx X-Axis speed * @param velocityy y-Axis speed * * Private void Recodeinfo(Final floatVelocityx,Final floatVELOCITYY) {FinalString info = String.Format (sformatstr, Velocityx, velocityy); Minfo.settext (info); } }
Velocitytracker Simple usage