TouchSlop,Velocity,GestureDector

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掉.






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值