0

我在我的 MapActivity 中实现了一个 OnTouchListener,当然我必须重写 onTouch() 方法。

我希望它仅在我点击地图时触发 - 否则我什至无法移动地图。

这是我的 onTouch 方法:

@Override
public boolean onTouch(View v, MotionEvent e) {

    if (e.getAction() == MotionEvent.ACTION_DOWN) {

      // SOME ACTIONS ...

    }
    return true;
}
4

3 回答 3

0

采用:

e.getAction() == MotionEvent.ACTION_UP

和:

e.getAction() == MotionEvent.ACTION_MOVE

如果在和 ACTION_DOWN 之后有 ACTION_UP 事件,并且之间没有 ACTION_MOVE,则它是一个水龙头

更新:

private int mLastMotionEventAction = MotionEvent.ACTION_UP;

@Override
public boolean onTouch(View v, MotionEvent e) {

    if (e.getAction() == MotionEvent.ACTION_DOWN) {


      // SOME DOWN ACTIONS ...

    } else if (e.getAction() == MotionEvent.ACTION_MOVE) {

      // SOME MOVE ACTIONS ...

    } else if (e.getAction() == MotionEvent.ACTION_UP) {
       if (mLastMotionEventAction == MotionEvent.ACTION_DOWN){

         // SOME TAP ACTIONS ...

       }
    }
    mLastMotionEventAction = e.getAction();
    return true;
}

另一种选择(用于敏感设备):

    private long mLastDownEventTime = 0;

    @Override
    public boolean onTouch(View v, MotionEvent e) {

        if (e.getAction() == MotionEvent.ACTION_DOWN) {
           mLastDownEventTime = System.currentTimeMillis();

          // SOME DOWN ACTIONS ...

        } else if (e.getAction() == MotionEvent.ACTION_MOVE) {

          // SOME MOVE ACTIONS ...

        } else if (e.getAction() == MotionEvent.ACTION_UP) {
           if (System.currentTimeMillis() - mLastDownEventTime < 500){

             // SOME TAP ACTIONS ...

           }
        }

        return true;
    }

仅当自上次按下后已过去 200 毫秒或更短时间时,才会触发点击操作。当然,您可以更改最佳 UX 的时间。

于 2012-09-27T09:25:07.940 回答
0

false如果您还想将事件传递给地图,则必须返回。否则,正如您所描述的,地图不会对触摸手势做出反应。这是因为,返回true意味着您消费了该事件。

于 2012-09-27T09:49:12.283 回答
0

我知道这是一个老问题,但正确的方法是使用getScaledTouchSlop()它返回以像素为单位的距离,在我们认为用户正在滚动之前,触摸可以徘徊。

在你的 onTouch 方法中像这样实现它:

@Override
    public boolean onTouch(final View v, MotionEvent event) {

        int mSwipeSlop = ViewConfiguration.get(context).
                    getScaledTouchSlop();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // DO
            break;
        case MotionEvent.ACTION_CANCEL:
            // DO
            break;
        case MotionEvent.ACTION_MOVE:
            {
                float x = event.getX() + v.getTranslationX();
                float deltaX = x - mDownX;
                float deltaXAbs = Math.abs(deltaX);
                float y = event.getY() + v.getTranslationY();
                float deltaY = Y - mDownY;
                float deltaYAbs = Math.abs(deltaY);
                float absDist = Math.sqrt(Math.pow(deltaXAbs, 2) + Math.pow(deltaXAbs, 2));
                if (!mSwiping) {
                    if (absDist > mSwipeSlop) {
                        mSwiping = true;
                    }
                }
            break;
        case MotionEvent.ACTION_UP:
            {
                if (mSwiping) {
                    // DO MOVE ACTIONS
                }
                else {
               // DO TAP ACTIONS
                }
            }
            break;
        default: 
            return false;
        }
        return true;
    };

开发者页面: http: //developer.android.com/reference/android/view/ViewConfiguration.html#getScaledTouchSlop()

于 2014-10-25T17:18:53.640 回答