In our ordinary use of which will only use a lot of gestures, DOWN, MOVE, UP, SCROLL, FLING and so on, of course, use the View in our previous understanding in onTouchEvent seem to be resolved, but perhaps logically implemented It will be very complicated. So Android easier for us to implement complex gestures provide GestureDetector this class.
DestureDetector interface
Providing two outside interfaces DestureDetector ( OnGestureDetector , OnDoubleTapListener ) and an outer class ( SimpleOnGestureListener ).
GestureDetector.OnGesstureListener Interface
First, we see that the method within the interface needs to be implemented as follows:
public interface OnGestureListener {
/**
*用户按下屏幕的时候就会触发
*/
boolean onDown(MotionEvent e);
/**
* 如果是按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPresss就会执行
*/
void onShowPress(MotionEvent e);
/**
*一次轻击屏幕,立刻抬起来,就会有这个触发,当然如果除了Down以外还有其他操作,那就不会再算是Single操作。所以也就不会触发这个事件。
*/
boolean onSingleTapUp(MotionEvent e);
/**
*在屏幕上拖动事件。无论是用手指慢慢拖动View,还是以快速的抛出动作滚动,都会多次触发这个方法
*/
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
/**
*长按屏幕,超过一定的时间就会触发这个事件
*/
void onLongPress(MotionEvent e);
/**
* 滑屏,用户按下触摸屏,快速移动后松开,由一个MotionEvent.ACTION_DOWN,多个ACTION_MOVE,1个ACTION_UP触发
*e1:第一个ACTION_DOWN MotionEvent
*e2:最后一个ACTION_MOVE MotionEvent
*velocityX:X轴上的移动距离,像素/秒
*velocityY:Y轴上的移动距离,像素/秒
*/
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
Click triggered all kinds of events:
- Click very fast (not sliding) Touchup: onDown-> onSingleTapup-> onSingleTapConfirmed
- Click a little slower (not sliding): onDown-> onSowPress-> onSingleTapup-> onSingleTapConfirmed
- When the sliding screen: onDown -----> onScroll-> onScroll ----> onScroll ----> ......... -----> onFling
- When you drag: onDown ------> onScroll ----> onScroll-> ... ------> onFiling
From this realization in the listener we know that this role is to monitor some gestures interface, click, slide, drag, press, etc.
GestureDetector.OnDoubleTapListener接口
Look at the internal interface implementation:
/**
*可以确认这是一个单击事件而不是一个双击事件的时候回调
*/
boolean onSingleTapConfirmed(MotionEvent e);
/**
* 可以确认这是一个双击事件的时候回调
*/
boolean onDoubleTap(MotionEvent e);
/**
*onDoubleTap()回调之后的输入事件(MOVE,UP,DOWN)都会回调这个方法
*这个方法可以实现一些双击后的控制,让View双击后变得可拖动等
*/
boolean onDoubleTapEvent(MotionEvent e);
By looking at the internal interface code we know that this listener listens to achieve the main event and double click event
GestureDetector.OnContextClickListener
Only such a method within this method:
/**
* 外接键盘的鼠标右键,当鼠标右键点击的时候回调
*/
boolean onContextClick(MotionEvent e)
GestureDetector.SimpleOnGestureListener接口
In fact, this interface is to achieve a comprehensive three methods above, with the top three interfaces of all callback methods
Steps for usage
- Step 1: Create OnGestureListener () function monitor
- Creating DestureDetector examples
- Intercept onTouch (View v, MotionEvent event) in
- Bound control
Here's use an example to show how to use it GestureDetector
xml file, then the file is only set up a TextView, but also in the java code we set this TextView can click and double-click.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="Hello World!"
android:layout_margin="50dp"
android:id="@+id/tv"
android:background="#ff00ff"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
java code, and implements the interface onGestureListener onDoubleTapListener
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector=new GestureDetector(new gestureListener());
mGestureDetector.setOnDoubleTapListener(new doubletapListener());
TextView tv=findViewById(R.id.tv);
tv.setOnTouchListener(this);
tv.setFocusable(true);
tv.setClickable(true);
tv.setLongClickable(true);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private class gestureListener implements GestureDetector.OnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
Log.e("gestureListener","onDown");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.e("gestureListener","onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.e("gestureListener","onSingleTapUp");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.e("gestureListener","onScroll"+(e2.getX()-e1.getX())+" "+distanceX);
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.e("gestureListener","onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("gestureListener","onFling");
return true;
}
}
private class doubletapListener implements GestureDetector.OnDoubleTapListener{
/**
* 单击事件
* @param e
* @return
*/
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.e("doubletapListener","onSingleTapConfirmed");
Toast.makeText(MainActivity.this, "onSingleTapConfirmed", Toast.LENGTH_SHORT).show();
return true;
}
/**
* @param e 双击事件
* @return
*/
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e("doubletapListener","onDoubleTap");
Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_SHORT).show();
return true;
}
/**
* 双击间隔中发生的动作
* @param e
* @return
*/
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.e("doubletapListener","doubletapListener");
Toast.makeText(MainActivity.this, "doubletapListener", Toast.LENGTH_SHORT).show();
return true;
}
}
}
The code is simple just print your log within each method:
Click Event:
Double-click Event:
Slide:
The above is the result of the implementation of what I click, double click, drag and other events, of course, we print the log in order to better use it, inside this method we can execute the method or event we need to use.
Use GestureDetector.SimpleOnGestureListener class
This is a class when using only extends, not implements, in its internal rewrite the onGestureListener and onDoubleTapListener all functions of the two interfaces, but these methods are empty. Using this class, we do not need to write all the way from, I only need to rewrite to use, it will feel to use code more concise.
The timing of each method call:
Callback / input events | DOWN event | MOVE event | UP event |
---|---|---|---|
onDown | Y | N | N |
onShowPress | Y | N | N |
onLongPress | Y | N | N |
onScroll | N | Y | N |
onFling | N | N | Y |
onSimgleTapUp | N | N | Y |
inSingleTapConfirmed | N | N | Y |
onDoubleTap | Y | N | N |
onDoubleTapEvent | Y | Y | Y |