Android--Velocitytracker

Source: Internet
Author: User

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

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.