无法在Android Surfaceview的自定义视图中停止矩阵的无限滚动

时间:2018-09-08 13:44:20

标签: android matrix canvas surfaceview translate

我正在制作室内地图,创建了一个表面视图自定义类,每件事都很好,但是问题是当我翻译地图时,图像会无限滚动,我正在使用缩放和缩小,条件是地图不应显示在屏幕之外,请帮助我。我的触摸事件代码附在这里。

 @Override
    public boolean onTouchEvent(MotionEvent event) {
    if (!isMapLoaded) {
        return false;
    }


    float newDist;


    switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:

            touchState = 0;
            saveMatrix.set(currentMatrix);
            startTouch.set(event.getX(), event.getY());
            currentTouchState = MapView.TOUCH_STATE_SCROLL;
            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            if (event.getPointerCount() == 2) {
                saveMatrix.set(currentMatrix);
                saveZoom = currentZoom;

                startTouch.set(event.getX(0), event.getY(0));
                currentTouchState = MapView.TOUCH_STATE_TWO_POINTED;

                mid = midPoint(event);

                oldDist = distance(event.getX(), event.getY(), mid);
            }
            break;

        case MotionEvent.ACTION_UP:


            if (withFloorPlan(event.getX(), event.getY())) {
                // layers on touch

                float deltaX = startTouch.x - event.getX();
                float deltaY = startTouch.y - event.getY();

                if (deltaX < 0)
                    deltaX *= -1;

                if (deltaY < 0)
                    deltaY *= -1;


                if (touchState == 0 || (deltaX < 50 && deltaY < 50)) {

                    isListenerCalled = true;


                    // Some work here

                }

            }
            currentTouchState = MapView.TOUCH_STATE_NO;
            break;


        case MotionEvent.ACTION_POINTER_UP:

            currentTouchState = MapView.TOUCH_STATE_NO;
            break;


        case MotionEvent.ACTION_MOVE:
            touchState = 2;
            switch (currentTouchState) {
                case MapView.TOUCH_STATE_SCROLL: {
                    currentMatrix.set(saveMatrix);
                    currentMatrix.postTranslate(event.getX() - startTouch.x, event.getY() - startTouch.y);
                    //  rebound();
                    refresh();
                }
                break;
                case MapView.TOUCH_STATE_TWO_POINTED: {

                    Log.d("states", "pointed " + event.getX() + " " + event.getY());

                    statePointed(event.getX(), event.getY());
                }

                break;
                case MapView.TOUCH_STATE_SCALE: {
                    Log.d("states", "scale");

                    currentMatrix.set(saveMatrix);
                    newDist = distance(event.getX(), event.getY(), mid);
                    float scale = newDist / oldDist;
                    if (scale * saveZoom < minZoom) {
                        scale = minZoom / saveZoom;
                    } else if (scale * saveZoom > maxZoom) {
                        scale = maxZoom / saveZoom;
                    }
                    currentZoom = scale * saveZoom;
                    currentMatrix.postScale(scale, scale, mid.x, mid.y);

                }
                break;

                default:
                    break;
            }
            break;
        default:
            break;
    }
    return true;
}

0 个答案:

没有答案