检测滑动和点击手势 [英] Detecting Swipe and Tap gestures

查看:128
本文介绍了检测滑动和点击手势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测onTouch方法中的滑动和点击?我希望在滑动时执行两个动作之一,而在我点击时执行另一个动作.需要知道如何减轻点击和滑动之间的差异.

How to detect Swipe and Tap in onTouch method? I want one of two actions to be executed when I swipe and other when I just tap. Need to know how to dell the difference between tap and swipe.

这是我的代码:

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
    mGestureDetector.onTouchEvent(event);
}
switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        x1 = event.getX();
        y1 = event.getY();
        break;
    case MotionEvent.ACTION_UP:
        x2 = event.getX();
        y2 = event.getY();

        float deltaX = x2 - x1;
        float deltaY = y2 - y1;

        if (Math.abs(deltaX) > MIN_DISTANCE) {
            mGestureDetector.onTouchEvent(event);
        }else if (Math.abs(deltaX) < MIN_DISTANCE) {
            mGestureDetector.onTouchEvent(event);
        }else if (Math.abs(deltaY) > MIN_DISTANCE) {
            mGestureDetector.onTouchEvent(event);
        }else if (Math.abs(deltaY) < MIN_DISTANCE) {
            mGestureDetector.onTouchEvent(event);
        }
        break;
}
return true;

}

推荐答案

Here is what you can do,found out on this link In onCreate of activity, add this.

final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
calendar.setOnTouchListener(new OnTouchListener() {
   @Override
   public boolean onTouch(final View view, final MotionEvent event) {
   if (gestureDetector.onTouchEvent(event)) {
      return false;
    }
    return true;
  }
});

这是GestureDetector类

And here is GestureDetector Class

class GestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
    // Trigger the touch event on the calendar
      calendar.onTouchEvent(event);
      return super.onSingleTapUp(event);
    }

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    ViewConfiguration viewConfiguration = ViewConfiguration.get(EventsActivity.this);
    int minSwipeDistance = viewConfiguration.getScaledPagingTouchSlop();
    int minSwipeVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
    int maxSwipeOffPath = viewConfiguration.getScaledTouchSlop();
    if (Math.abs(e1.getY() - e2.getY()) > maxSwipeOffPath) {
        return false;
    }

   if (Math.abs(velocityX) > minSwipeVelocity) {
   // Right to left swipe
      if (e1.getX() - e2.getX() > minSwipeDistance) {
      calendar.nextMonth();
   }

   // Left to right
   else if (e2.getX() - e1.getX() > minSwipeDistance) {
        calendar.previousMonth();
   }

    // Call some app-related functions to update the display
    displayDate(calendar.getMonth(), calendar.getYear());
  }
  return false;
 }
}

这篇关于检测滑动和点击手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆