Activity_main.xml
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Tools:context= "Sowell.oracle.com.view.MainActivity">7 <!--8 TV is used to show how fast your finger moves9 -Ten <TextView One Android:id= "@+id/tv" A Android:text= "AAA" - Android:layout_width= "100DP" - Android:layout_height= "Match_parent" /> the </LinearLayout>
Mainactivity.java
1 PackageSowell.oracle.com.view;2 3 Importandroid.support.v7.app.AppCompatActivity;4 ImportAndroid.os.Bundle;5 Importandroid.view.MotionEvent;6 ImportAndroid.view.VelocityTracker;7 ImportAndroid.view.View;8 ImportAndroid.widget.EditText;9 ImportAndroid.widget.TextView;Ten One Public classMainactivityextendsappcompatactivity { A - PublicVelocitytracker Velocitytracker; - PublicTextView TV; the Public Doublexvelocity; - Public Doubleyvelocity; - - Public voidinit () { +tv=(TextView) Findviewbyid (r.id.tv); -Velocitytracker=Velocitytracker.obtain (); + } A at - @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - init (); inTv.settext ("BBB"); - } to + - //rewrite the callback function of the Click event the @Override * Public Booleanontouchevent (Motionevent event) { $ Panax Notoginseng Final intAction =event.getaction (); -Velocitytracker=Velocitytracker.obtain (); the velocitytracker.addmovement (event); + A the Switch(action) { + CaseMotionevent.action_move: -Velocitytracker.computecurrentvelocity (1000);//calculate the pixels per 1000ms finger movement $xvelocity=velocitytracker.getxvelocity (); $yvelocity=velocitytracker.getyvelocity (); - Add (xvelocity,yvelocity); - Break; the - Casemotionevent.action_up:WuyiVelocitytracker.clear ();//Recycling Velocitytracker the velocitytracker.recycle (); - Break; Wu - CaseMotionevent.action_cancel: About velocitytracker.clear (); $ velocitytracker.recycle (); - Break; - - } A + return Super. Ontouchevent (event); the } - $ the the the the voidAddDoubleXDoubley) - { in FinalString Info=string.format ("Horizontal speed:%f\n vertical speed:%f", x, y); the Tv.settext (info); the } About}
Velocitytracker detection of finger movement speed