您的位置:首页 > 移动开发 > Android开发

Android手势检测GestureDetector

2016-11-28 16:55 302 查看
现在的智能机应该是100%触屏手机了。当我们触摸手机的时候,发生了一些操作如:action_up,action_down,action_move,action_cancle。我们可以通过setonTouchListener来处理这些操作。但ontouch处理一些复杂的手势就比较麻烦了。什么单击,长按,单击后长按前等等等等。此时,我们就需要用到手势检测:GestureDetector这个类。然后下午就花了点时间稍微看了下,原本打算看完之后在整理的。结果发现需要边写代码边整理,就让我和你们一起开始慢慢了解GestureDetector手势检测把。

GestureDetector接口

GestureDetector给我们提供了三个接口,OnGestureListener,OnDoubleTapListener以及SimpleOnGestureListener。

OnGestureListener

首先让我们new一个OnGestureListener来实现他的方法:

new GestureDetector.OnGestureListener() {

//刚刚手指接触到触摸屏的那一刹那,就是触的那一下。
public boolean onDown(MotionEvent e) {
Log.i(TAG, "onDown: 刚刚触摸到按钮");
return true;
}

// 手指按在触摸屏上,它的时间范围在按下起效,在长按之前。
public void onShowPress(MotionEvent e) {
Log.i(TAG, "onShowPress: 手指放在按钮上");
}

//手指离开触摸屏的那一刹那。
public boolean onSingleTapUp(MotionEvent e) {
Log.i(TAG, "onSingleTapUp: 手指离开了按钮");
return true;
}

// 手指在触摸屏上滑动。
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i(TAG, "onScroll: 手指在按钮上滑动");
return true;
}

//手指按在持续一段时间,并且没有松开。
public void onLongPress(MotionEvent e) {
Log.i(TAG, "onLongPress: 长按了按钮");
}

// 手指在触摸屏上迅速移动,并松开的动作。
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.i(TAG, "onFling: 手指在按钮上移动,并且松开了");
return true;
}


相信我的注解已经很清楚了。现在让我们来做个测试,点击然后松开:



此时,我们继续来个长按按钮:



问题来了。当我们离开屏幕时为什么没有打印离开屏幕的log呢?

我们来看下onSingleTapUp(MotionEvent event)这个方法。singletapup,顾名思义,这是一个单击的抬起操作。也就是点击屏幕后,立即离开。才会触发这个方法。所以我们调用onlongPress。无法触发此事件。

好了。继续我们的操作,来一次移动后离开的触摸。





测试了两次,按照打印的顺序,我们发现没有什么问题。

OnDoubleTapListener

这是一个双击事件的监听,我们来new一个玩玩看看如何~~~

detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
//单击事件
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i(TAG, "onSingleTapConfirmed: 单击了按钮");
return true;
}
//双击事件
public boolean onDoubleTap(MotionEvent e) {
Log.i(TAG, "onDoubleTap: 双击了按钮");
return true;
}

//触发onDoubleTap以后,在双击之间发生的其它动作
public boolean onDoubleTapEvent(MotionEvent e) {
Log.i(TAG, "onDoubleTapEvent: 发生了其他操作");
return false;
}
});


先来个测试:





单击,双击,都没有问题。

此处,我有一个疑问onDoubleTapEvent这是一个触发了双击后发生的事件,是不是能理解为,双击后,手指离开屏幕的这个事件就被认作是其他操作呢?我们来继续证实一下,现在我们双击后,不离开屏幕看看发生了什么。



此时进行双击后,然后不松开,发生了长按事件,相当于我们手指按下,移动。松开,都属于其他操作。看来我的理解还算正确的。

SimpleOnGestureListener

这个接口,可以说,是最常用的了。为什么呢?因为他是综合了上述两种接口而实现的,我们看下源码:

public static class SimpleOnGestureListener implements OnGestureListener, OnDoubleTapListener {
public boolean onSingleTapUp(MotionEvent e) {
return false;
}

public void onLongPress(MotionEvent e) {
}

public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return false;
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}

public void onShowPress(MotionEvent e) {
}

public boolean onDown(MotionEvent e) {
return false;
}

public boolean onDoubleTap(MotionEvent e) {
return false;
}

public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}

public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
}


就是酱紫~,为什么说是最常用的呢。因为,在做事件处理的时候,我们会发现有的方法是用不到的。但为了实现这个接口,又不得不写。但SimpleOnGestureListener就解决了这个问题,我们来继续做个测试:

detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
Log.i(TAG, "onLongPress: 长按了按钮");
}

public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i(TAG, "onSingleTapConfirmed: 单击了按钮");
return true;
}
});
}




好了。

下面完整的代码,大家可以自己去试试:


@EActivity(R.layout.detector)
public class GestureDetectorActivity extends Activity implements View.OnTouchListener {

@ViewById
Button btn;
GestureDetector detector;
private static final String TAG = "GestureDetectorActivity";

@AfterViews
void afterInitView() {
btn.setOnTouchListener(this);
// detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
//
// //刚刚手指接触到触摸屏的那一刹那,就是触的那一下。
// public boolean onDown(MotionEvent e) {
// Log.i(TAG, "onDown: 刚刚触摸到按钮");
// return true;
// }
//
// // 手指按在触摸屏上,它的时间范围在按下起效,在长按之前。
// public void onShowPress(MotionEvent e) {
// Log.i(TAG, "onShowPress: 手指放在按钮上");
// }
//
// //手指离开触摸屏的那一刹那。
// public boolean onSingleTapUp(MotionEvent e) {
// Log.i(TAG, "onSingleTapUp: 手指离开了按钮");
// return true;
// }
//
// // 手指在触摸屏上滑动。
// public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// Log.i(TAG, "onScroll: 手指在按钮上滑动");
// return true;
// }
//
// //手指按在持续一段时间,并且没有松开。
// public void onLongPress(MotionEvent e) {
// Log.i(TAG, "onLongPress: 长按了按钮");
// }
//
// // 手指在触摸屏上迅速移动,并松开的动作。
// public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Log.i(TAG, "onFling: 手指在按钮上移动,并且松开了");
// return true;
// }
// });
// detector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
// //单击事件
// public boolean onSingleTapConfirmed(MotionEvent e) {
// Log.i(TAG, "onSingleTapConfirmed: 单击了按钮");
// return true;
// }
// //双击事件
// public boolean onDoubleTap(MotionEvent e) {
// Log.i(TAG, "onDoubleTap: 双击了按钮");
// return true;
// }
//
// //触发onDoubleTap以后,在双击之间发生的其它动作
// public boolean onDoubleTapEvent(MotionEvent e) {
// Log.i(TAG, "onDoubleTapEvent: 发生了其他操作");
// return false;
// }
// });detector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() { public void onLongPress(MotionEvent e) { Log.i(TAG, "onLongPress: 长按了按钮"); } public boolean onSingleTapConfirmed(MotionEvent e) { Log.i(TAG, "onSingleTapConfirmed: 单击了按钮"); return true; } }); }

public boolean onTouch(View v, MotionEvent event) {
return detector.onTouchEvent(event);
}
}


如果没用过EActivity的,自己在oncreate设置下布局,afterview()方法其实就是初始化操作。

好了。有什么不懂的可以提出来。大家一起讨论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: