您的位置:首页 > 其它

手势识别

2016-06-10 17:13 274 查看
基本原理:

通过为控件设置监听器,获取到控件的MotionEvent,然后交给GestureDetector处理,而GestureDetector实例化的时候必须传入一个监听器以在必要的时候可以回掉其中的方法





而安卓系统已经有类SimpleOnGestureListener实现了上述的两个接口,我们只需要继承这个类重写响音的方法就可以了。

Eg:

// 实例化手势识别器,传入监听器
detector = new GestureDetector(new yang());
// 为图片设置监听器,捕获事件
image.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View arg0, MotionEvent e) {
// 把捕获到的事件交给手势识别器类处理
detector.onTouchEvent(e);
return true;
}
});
}

private class yang extends SimpleOnGestureListener {
// 此类的内部已经对事件进行了相应的分析了,
// 我们只需要重写相应的方法,供发生这个事件的时候回掉就可以了
@Override
public boolean onFling(MotionEvent e_down, MotionEvent e_up,
float velocityX, float velocityY) {
if (e_down.getX() - e_up.getX() > 30) {
System.out.println("从右边向左边滑动了");
} else if (e_up.getX() - e_down.getX() > 30) {
System.out.println("从做想右边边滑动了");
}
return super.onFling(e_down, e_up, velocityX, velocityY);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: