OnTouchEvent,如果我触摸场景中的某个地方,我的场景会回来

时间:2017-07-18 09:29:06

标签: android ontouchevent motionevent

我的问题可能很奇怪,但我无法理解为什么我的场景会向后移动,如果我在其他地方触摸屏幕,请帮我解决。

https://youtu.be/oKdwqn0HC0Y - 在此视频中记录我的问题, 为了更好地理解问题

 @Override
        public boolean onTouchEvent(MotionEvent event) {
            final int action = event.getAction();
            if (action == MotionEvent.ACTION_DOWN) {
                return true;
            }
            switch (event.getPointerCount()) {
                case 3:
                    Log.e("Event", "PointerCount = " +event.getPointerCount());
                    return mScaleDetector.onTouchEvent(event);
                case 2:
                    Log.e("Event", "PointerCount = " +event.getPointerCount());
                    return doRotationEvent(event);
                case 1:
                    Log.e("Event", "PointerCount = " +event.getPointerCount());
                    return doMoveEvent(event);
            }
            return true;
        }

        private boolean doMoveEvent(MotionEvent event)
        {
            final int action = event.getAction();
            Log.e("doMoveEvent", "action = " +action);

            switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    Log.e("doMoveEvent", "action_down");
                    final float x = event.getX();
                    Log.e("doMoveEvent", " x = " +x);
                    final float y = event.getY();
                    Log.e("doMoveEvent", " y = " +y);
                    // Remember where we started
                    mLastTouchX = x;
                    mLastTouchY = y;
                    break;
                }

                case MotionEvent.ACTION_MOVE: {
                    final float x = event.getX();
                    final float y = event.getY();
                        // Calculate the distance moved
                        final float dx = x - mLastTouchX;
                        final float dy = y - mLastTouchY;
                        Log.e("doMoveEvent", " DX = " +dx);
                        Log.e("doMoveEvent", " DY = " +dy);

                        renderer.angleX += dy;
                        renderer.angleY += dx;

                    mLastTouchX = x;
                    mLastTouchY = y;

                    // Invalidate to request a redraw
                    break;
                }

                case MotionEvent.ACTION_UP: {
                    mActivePointerId = INVALID_POINTER_ID;
                    break;
                }

                case MotionEvent.ACTION_CANCEL: {
                    mActivePointerId = INVALID_POINTER_ID;
                    break;
                }
            }
            return true;
        }

1 个答案:

答案 0 :(得分:0)

我用这种方式解决了我的问题

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            final float x = event.getX();   //(NEW)
            final float y = event.getY();   //(NEW)

            mLastTouchX = x;    //(NEW)
            mLastTouchY = y;    //(NEW)
            return true;
        }
        switch (event.getPointerCount()) {
            case 3:
                Log.e("Event", "PointerCount = " +event.getPointerCount());
                return mScaleDetector.onTouchEvent(event);
            case 2:
                Log.e("Event", "PointerCount = " +event.getPointerCount());
                return doRotationEvent(event);
            case 1:
                Log.e("Event", "PointerCount = " +event.getPointerCount());
                return doMoveEvent(event);
        }
        return true;
    }