为了加强点击、拖动响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:
Java代码
- boolean onDoubleTap(MotionEvent e)
解释:双击的第二下down时触发
Java代码
- boolean onDoubleTapEvent(MotionEvent e)
解释:双击的第二下down和up都会触发,可用e.getAction()区分。
Java代码
- boolean onDown(MotionEvent e)
解释:down时触发
Java代码
- boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
Java代码
- void onLongPress(MotionEvent e)
解释:Touch了不移动一直down时触发
Java代码
- boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
Java代码
- void onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
与onDown,onLongPress比较 onDown只要Touch down一定立刻触发。
而Touch down后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touch down后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。
boolean onSingleTapConfirmed(MotionEvent e)
boolean onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
创建手势监听对象:
Java代码
- package com.sun.gesturetest;
- import android.util.Log;
- import android.view.GestureDetector.SimpleOnGestureListener;
- import android.view.MotionEvent;
- public class MyGestureListener extends SimpleOnGestureListener {
- private static final String TAG = "MyGestureListener";
- public MyGestureListener() {
- }
- /**
- * 双击的第二下Touch down时触发
- * @param e
- * @return
- */
- @Override
- public boolean onDoubleTap(MotionEvent e) {
- Log.i(TAG, "onDoubleTap : " + e.getAction());
- return super.onDoubleTap(e);
- }
- /**
- * 双击的第二下 down和up都会触发,可用e.getAction()区分。
- * @param e
- * @return
- */
- @Override
- public boolean onDoubleTapEvent(MotionEvent e) {
- Log.i(TAG, "onDoubleTapEvent : " + e.getAction());
- return super.onDoubleTapEvent(e);
- }
- /**
- * down时触发
- * @param e
- * @return
- */
- @Override
- public boolean onDown(MotionEvent e) {
- Log.i(TAG, "onDown : " + e.getAction());
- return super.onDown(e);
- }
- /**
- * Touch了滑动一点距离后,up时触发。
- * @param e1
- * @param e2
- * @param velocityX
- * @param velocityY
- * @return
- */
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- Log.i(TAG, "onFling e1 : " + e1.getAction() + ", e2 : " + e2.getAction() + ", distanceX : " + velocityX + ", distanceY : " + velocityY);
- return super.onFling(e1, e2, velocityX, velocityY);
- }
- /**
- * Touch了不移动一直 down时触发
- * @param e
- */
- @Override
- public void onLongPress(MotionEvent e) {
- Log.i(TAG, "onLongPress : " + e.getAction());
- super.onLongPress(e);
- }
- /**
- * Touch了滑动时触发。
- * @param e1
- * @param e2
- * @param distanceX
- * @param distanceY
- * @return
- */
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- Log.i(TAG, "onScroll e1 : " + e1.getAction() + ", e2 : " + e2.getAction() + ", distanceX : " + distanceX + ", distanceY : " + distanceY);
- return super.onScroll(e1, e2, distanceX, distanceY);
- }
- /**
- * Touch了还没有滑动时触发
- * @param e
- */
- @Override
- public void onShowPress(MotionEvent e) {
- Log.i(TAG, "onShowPress : " + e.getAction());
- super.onShowPress(e);
- }
- @Override
- public boolean onSingleTapConfirmed(MotionEvent e) {
- Log.i(TAG, "onSingleTapConfirmed : " + e.getAction());
- return super.onSingleTapConfirmed(e);
- }
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- Log.i(TAG, "onSingleTapUp : " + e.getAction());
- return super.onSingleTapUp(e);
- }
- }
在Activity中onTouchEvent(MotionEvent event)方法中设置手势:
Java代码
- package com.sun.gesturetest;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.GestureDetector;
- import android.view.Menu;
- import android.view.MotionEvent;
- public class MainActivity extends Activity {
- private GestureDetector mDetector;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mDetector = new GestureDetector(this, new MyGestureListener());
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- return mDetector.onTouchEvent(event);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
或者在自定义View中设置手势:
Java代码
- package com.sun.gesturetest;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.view.GestureDetector;
- import android.view.MotionEvent;
- import android.view.View;
- public class GestureView extends View {
- private GestureDetector mDetector;
- public GestureView(Context context, AttributeSet set) {
- super(context, set);
- mDetector = new GestureDetector(context, new MyGestureListener());
- setLongClickable(true);
- this.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return mDetector.onTouchEvent(event);
- }
- });
- }
- }
在View中设置手势有两点需要注意:
1:View必须设置longClickable为true,否则手势识别无法正确工作,只会返回Down, Show, Long三种手势。
2:必须在View的onTouchListener中调用手势识别,而不能像Activity一样重载onTouchEvent,否则同样手势识别无法正确工作。