1.初识gesture:
Android提供的手势:GestureDetector.OnGestureListener。
If you only want to listen for a subset it might be easier to extend GestureDetector.SimpleOnGestureListener.
基本的手势监听包括:
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发 public boolean onDown(MotionEvent arg0) {
/* * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 * 注意和onDown()的区别,强调的是没有松开或者拖动的状态 */ public void onShowPress(MotionEvent e) {
// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发 public boolean onSingleTapUp(MotionEvent e) {
// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发 public void onLongPress(MotionEvent e) {
public boolean onDoubleTap(MotionEvent e) { //双击时产生一次
public boolean onDoubleTapEvent(MotionEvent e) { //双击时产生两次
触发手势监听的方法:
mGestureDetector = new GestureDetector(this);
在onTouch方法中,触发GestureDetector触发。
public boolean onTouchEvent(MotionEvent ev) { mGestureDetector.onTouchEvent(ev)
遇到的问题
1.持续滑动,怎么判断onScroll结束?
if (mGestureDetector.onTouchEvent(ev)) return true; if (ev.getAction() == MotionEvent.ACTION_UP) { if (mIsScrolling) { mIsScrolling = false; handleScrollFinished(); } } return false; }
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if (!mIsScrolling) {
mIsScrolling = true;}
2.长按后再滑动无法触发onScroll
public void setIsLongpressEnabled (boolean isLongpressEnabled)
Set whether longpress is enabled, if this is enabled when a user presses and holds down you get a longpress event and nothing further. If it's disabled the user can press and hold down and then later moved their finger and you will get scroll events. By default longpress is enabled.
3.onFling需要注意的地方
onFling触发的时候,如果加打印的话,可以先看到触发onScroll事件,然后触发onFling事件,所以在onFling结束的时候,需要onScroll的暂态数据全部清空。
4.注意onScroll,onFling参数distance的具体含义
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
Notified of a fling event when it occurs with the initial on down MotionEvent
and the matching up MotionEvent
. The calculated velocity is supplied along the x and y axis in pixels per second.
Parameters
e1 | The first down motion event that started the fling. |
---|---|
e2 | The move motion event that triggered the current onFling. |
velocityX | The velocity of this fling measured in pixels per second along the x axis. |
velocityY | The velocity of this fling measured in pixels per second along the y axis. |
public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
Notified when a scroll occurs with the initial on down MotionEvent
and the current move MotionEvent
. The distance in x and y is also supplied for convenience.
Parameters
e1 | The first down motion event that started the scrolling. |
---|---|
e2 | The move motion event that triggered the current onScroll. |
distanceX | The distance along the X axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2 . |
distanceY | The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2 . |