码迷,mamicode.com
首页 > 移动开发 > 详细

VelocityTracker检测手指的移动速度

时间:2017-11-11 23:53:21      阅读:418      评论:0      收藏:0      [点我收藏+]

标签: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     <TextView
11         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     @Override
25     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     @Override
35     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();                        //回收VelocityTracker
52                 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检测手指的移动速度

标签:put   .text   view   boolean   xmlns   bundle   pat   compute   package   

原文地址:http://www.cnblogs.com/zhengzhe/p/7820303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!