前言
这次主要讲解一些Android简单手势的识别,主要用到的是GestureDetector,SimpleOnGestureListener,OnGestureListener等类
实现原理
我们想要实现手势的识别,当然要监听触摸屏事件,主要流程是:重写View的onTouchEvent事件,将拦截到的MotionEvent事件交给GestureDetector类的OnGestureListener和OnDoubleTapListener处理。我们可以分别处理OnGestureListener和OnDoubleTapListener中的事件,因为我们不会用到所有的事件,所以我们也可以直接使用GestureDetector.SimpleOnGestureListener,这是一个实现了OnGestureListener接口和OnDoubleTapListener接口的类。
拦截事件
如果我们要拦截整个Activity的触摸屏事件,重写onTouchEvent(MotionEvent event)即可,当然我们可以为单独的 View设置监听事件,view.setOnTouchListener,为View添加监听事件,然后将MotionEvent事件分发给GestureDetector处理mGestureDetector.onTouchEvent(event);
处理事件
SimpleOnGestureListener中的事件:
boolean onDoubleTap(MotionEvent e)
// Notified when a double-tap occurs.
boolean onDoubleTapEvent(MotionEvent e)
//Notified when an event within a double-tap gestureoccurs, including the down, move, and up events.
boolean onDown(MotionEvent e)
// Notified when a tap occurs with the down MotionEvent that triggered it.
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.
void onLongPress(MotionEvent e)
//Notified when a long press occurs with the initial on down MotionEvent that trigged it.
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.
void onShowPress(MotionEvent e)
//The user has performed a down MotionEvent and not performed a move or up yet.
boolean onSingleTapConfirmed(MotionEvent e)
// Notified when a single-tap occurs.
boolean onSingleTapUp(MotionEvent e)
// Notified when a tap occurs with the up MotionEvent that triggered it.
英语太渣,不给你们翻译了 - -。
示例代码
GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
mGestureDetector = new GestureDetector(this, new MyGestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return mGestureDetector.onTouchEvent(event);
}
class MyGestureListener extends SimpleOnGestureListener
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_SHORT).show();
return super.onDoubleTap(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_SHORT).show();
return super.onDoubleTapEvent(e);
}
@Override
public boolean onDown(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_SHORT).show();
if (e1.getX() - e2.getX() > 50)
{
Toast.makeText(MainActivity.this, "从右向左划", Toast.LENGTH_SHORT)
.show();
} else if (e2.getX() - e1.getX() > 50)
{
Toast.makeText(MainActivity.this, "从左向右划", Toast.LENGTH_SHORT)
.show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_SHORT).show();
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_SHORT).show();
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public void onShowPress(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
super.onShowPress(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onSingleTapConfirmed", Toast.LENGTH_SHORT).show();
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
return super.onSingleTapUp(e);
}
}
这是简单手势的识别,下一篇讲解一下自定义的手势识别。
下篇:自定义手势识别