TouchSlop,Velocity,GestureDector

来源:互联网 发布:java 类似于枚举 编辑:程序博客网 时间:2024/05/22 01:58

TouchSlop  

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

ViewConfiguration.get(getContext()).getScaledTouchSlop();

Velocity 

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

@Overridepublic 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接口.

@Overridepublic 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接口,所以我们就可以在回调的接口方法里面去做我们自己想到的实现.

@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// TODO Auto-generated method stubfloat x = e1.getX();float x2 = e2.getX();if (x2-x>200) {finish();}return true;}
我实现了其中一个滑动的方法,当判断右滑的距离超过了200个像素点,就将当前页面给finish掉.






0 0
原创粉丝点击