您的位置:首页 > 运维架构

Tou 4000 chSlop,Velocity,GestureDector

2016-03-04 10:06 232 查看
TouchSlop  

系统所能识别出的被认为是滑动的最小距离,即当手指滑动的距离没有超过该最小距离,系统不认为使用者进行了滑动.该值会因为设备的不同而不同.通过下面的方法即可获得该常量值.
ViewConfiguration.get(getContext()).getScaledTouchSlop();

Velocity 
速度追踪,用于追踪手指在滑动过程中的速度,包括水平和竖直方向的速度.

使用方法:在view的onTouchEvent方法中.
@Override
public boolean onTouchEvent(MotionEvent event) {
VelocityTracker obtain = VelocityTracker.obtain();
obtain.addMovement(event);
obtain.computeCurrentVelocity(1000); //该1000是个毫秒值,代表要计算出在1秒以内的速度值
float xVelocity = obtain.getXVelocity();
float yVelocity = obtain.getYVelocity();
Log.e("MC", xVelocity+":"+yVelocity);
obtain.clear();
obtain.recycle();//如果不使用了,必须回收
}上面就可以获取到横竖方向上的速度. 

GestureDetector

手势检测,用于辅助检测用户的单击,滑动,长按,双击等行为.

使用方法:先创建GestureDector对象,并实现OnGestureListener接口.

@Override
public boolean onTouchEvent(MotionEvent event) {
GestureDetector detector = new GestureDetector(this,this); <span style="white-space:pre"> </span> //创建GestureDector对象,
detector.setIsLongpressEnabled(false);<span style="white-space:pre"> </span>//该方法可以解决长按屏幕后无法拖动的现象.
boolean onTouchEvent = detector.onTouchEvent(event);<span style="white-space:pre"> </span>//接管了目标view的onTouchEvent方法
return onTouchEvent;
}由于创建GestureDector对象的时候让该Acitivity页面实现了OnGestureListener接口,所以我们就可以在回调的接口方法里面去做我们自己想到的实现.
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub

float x = e1.getX();
float x2 = e2.getX();
if (x2-x>200) {
finish();
}
return true;
}我实现了其中一个滑动的方法,当判断右滑的距离超过了200个像素点,就将当前页面给finish掉.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: