VelocityTracker的用法
2016-03-11 11:16
525 查看
VelocityTracker在API中解释如下:
Helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use
方法如下:
![](https://img-blog.csdn.net/20160311111300433)
简单的Demo(我已经把网上的demo代码竟可能的简化了,这样看起来清晰一些)
package com.example.velocitytrackertest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
public class MainActivity extends Activity {
private VelocityTracker velocityTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//必须放这里,放在ACTION_DOWN里面XY输出为0
if(velocityTracker == null){
velocityTracker = VelocityTracker.obtain();//必须和recycle()配对
}
velocityTracker.addMovement(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.computeCurrentVelocity(1000);
int X = (int)velocityTracker.getXVelocity();
int Y = (int)velocityTracker.getYVelocity();
Log.i("X", X + "");
Log.i("Y", Y + "");
break;
case MotionEvent.ACTION_UP:
if(velocityTracker != null){
velocityTracker.recycle();
velocityTracker = null;
}
break;
}
return super.onTouchEvent(event);
}
}
Helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use
obtain()to retrieve a new instance of the class when you are going to begin tracking, put the motion events you receive into it with
addMovement(MotionEvent), and when you want to determine the velocity call
computeCurrentVelocity(int)and then
getXVelocity()and
getYVelocity().
方法如下:
简单的Demo(我已经把网上的demo代码竟可能的简化了,这样看起来清晰一些)
package com.example.velocitytrackertest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
public class MainActivity extends Activity {
private VelocityTracker velocityTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//必须放这里,放在ACTION_DOWN里面XY输出为0
if(velocityTracker == null){
velocityTracker = VelocityTracker.obtain();//必须和recycle()配对
}
velocityTracker.addMovement(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.computeCurrentVelocity(1000);
int X = (int)velocityTracker.getXVelocity();
int Y = (int)velocityTracker.getYVelocity();
Log.i("X", X + "");
Log.i("Y", Y + "");
break;
case MotionEvent.ACTION_UP:
if(velocityTracker != null){
velocityTracker.recycle();
velocityTracker = null;
}
break;
}
return super.onTouchEvent(event);
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories