VelocityTracker檢測手指的移動速度

來源:互聯網
上載者:User

標籤:put   .text   view   boolean   xmlns   bundle   pat   compute   package   

activity_main.xml

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns: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用於顯示手指移動的速度 9     -->10     <TextView11         android:id="@+id/tv"12         android:text="aaa"13         android:layout_width="100dp"14         android:layout_height="match_parent" />15 </LinearLayout>

 

MainActivity.java

 1 package sowell.oracle.com.view; 2  3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.MotionEvent; 6 import android.view.VelocityTracker; 7 import android.view.View; 8 import android.widget.EditText; 9 import android.widget.TextView;10 11 public class MainActivity extends AppCompatActivity {12 13     public VelocityTracker velocityTracker;14     public TextView tv;15     public double xVelocity;16     public double yVelocity;17 18     public void init(){19         tv=(TextView)findViewById(R.id.tv);20         velocityTracker=VelocityTracker.obtain();21     }22 23 24     @Override25     protected void onCreate(Bundle savedInstanceState) {26         super.onCreate(savedInstanceState);27         setContentView(R.layout.activity_main);28         init();29         tv.setText("bbb");30     }31 32 33     //重寫點擊事件的回呼函數34     @Override35     public boolean onTouchEvent(MotionEvent event) {36 37         final int action =event.getAction();38         velocityTracker=VelocityTracker.obtain();39         velocityTracker.addMovement(event);40 41 42         switch (action){43             case MotionEvent.ACTION_MOVE:44                 velocityTracker.computeCurrentVelocity(1000);   //計算每1000ms手指移動的像素45                 xVelocity=velocityTracker.getXVelocity();46                 yVelocity=velocityTracker.getYVelocity();47                 add(xVelocity,yVelocity);48                 break;49 50             case MotionEvent.ACTION_UP:51                 velocityTracker.clear();                        //回收VelocityTracker52                 velocityTracker.recycle();53                 break;54 55             case MotionEvent.ACTION_CANCEL:56                 velocityTracker.clear();57                 velocityTracker.recycle();58                 break;59 60         }61 62         return super.onTouchEvent(event);63     }64 65 66 67 68 69     void add(double x,double y)70     {71         final String info=String.format("水平速度:%f\n豎直速度:%f",x,y);72         tv.setText(info);73     }74 }

 

VelocityTracker檢測手指的移動速度

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.