我们常说的手势包括:单击、长按、双击、滑动、缩放, android提供了几个手势探测类:GestureDetector(点击类探测),ScaleGestureDetector(缩放探测),
如何用这几种探测类来捕捉用户的手势呢?
- 首先构建一个GestureDetector对象,创建GestureDetector对象时会传入一个回调对象(实现GestureDetector.OnGestureListener)
- view有一个虚拟函数onTouchEvent(MotionEvent), 只要重载这个函数,并将MotionEvent对象丢给GestureDetector对象就可以了
- GestureDetector类中会自己判断MotionEvent的事件类型,并回调
- 问题来了,怎么回调业务的接口?
- 回到1中,传入的回调对象其实是我们需要实现的,并继承GestureDetector中的接口来实现手势的回调
- 如果是缩放,将GestureDetector换成ScaleGestureDetector对象即可
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract boolean |
onDown(
MotionEvent e)
Notified when a tap occurs with the down
MotionEvent that triggered it.
| ||||||||||
abstract 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 .
| ||||||||||
abstract void |
onLongPress(
MotionEvent e)
Notified when a long press occurs with the initial on down
MotionEvent that trigged it.
| ||||||||||
abstract 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 .
| ||||||||||
abstract void |
onShowPress(
MotionEvent e)
The user has performed a down
MotionEvent and not performed a move or up yet.
| ||||||||||
abstract boolean |
onSingleTapUp(
MotionEvent e)
Notified when a tap occurs with the up
MotionEvent that triggered it.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract boolean |
onDoubleTap(
MotionEvent e)
Notified when a double-tap occurs.
| ||||||||||
abstract boolean |
onDoubleTapEvent(
MotionEvent e)
Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
| ||||||||||
abstract boolean |
onSingleTapConfirmed(
MotionEvent e)
Notified when a single-tap occurs.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract boolean |
onScale(
ScaleGestureDetector detector)
Responds to scaling events for a gesture in progress.
| ||||||||||
abstract boolean |
onScaleBegin(
ScaleGestureDetector detector)
Responds to the beginning of a scaling gesture.
| ||||||||||
abstract void |
onScaleEnd(
ScaleGestureDetector detector)
Responds to the end of a scale gesture.
|
示例代码:
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MyView extends View {
GestureDetector mGesture = null;
public MyView(Context context)
{
super(context);
mGesture = new GestureDetector(this.getContext(), new MyGestureDetector());
Log.d("MyGestureDetector", "MyView()");
}
protected void onDraw(Canvas canvas)
{
android.util.Log.d("MyGestureDetector", "onDraw");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("MyGestureDetector", "onTouch");
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
class MyGestureDetector implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener
{
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
/*或者直接这种继承也为常见
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
@Override
public void onLongPress(MotionEvent e)
{
Log.d("MyGestureDetector", "onLongPress");
super.onLongPress(e);
}
}
*/
}