diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java | 103 |
1 files changed, 40 insertions, 63 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java index c9e6e633a..9473ea709 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java @@ -74,7 +74,7 @@ final class MapGestureDetector { * User-set focal point. */ @Nullable - private PointF focalPoint; + private PointF constantFocalPoint; private AndroidGesturesManager gesturesManager; @@ -158,7 +158,7 @@ final class MapGestureDetector { /** * Set the gesture focal point. * <p> - * this is the center point used for calculate transformations from gestures, value is + * This is the center point used for calculate transformations from gestures, value is * overridden if end user provides his own through {@link UiSettings#setFocalPoint(PointF)}. * </p> * @@ -172,22 +172,7 @@ final class MapGestureDetector { focalPoint = uiSettings.getFocalPoint(); } } - this.focalPoint = focalPoint; - } - - /** - * Get the current active gesture focal point. - * <p> - * This could be either the user provided focal point in - * {@link UiSettings#setFocalPoint(PointF)}or <code>null</code>. - * If it's <code>null</code>, gestures will use focal pointed returned by the detector. - * </p> - * - * @return the current active gesture focal point. - */ - @Nullable - PointF getFocalPoint() { - return focalPoint; + this.constantFocalPoint = focalPoint; } /** @@ -357,18 +342,25 @@ final class MapGestureDetector { int action = motionEvent.getActionMasked(); if (action == MotionEvent.ACTION_DOWN) { executeDoubleTap = true; + + // disable the move detector in preparation for the quickzoom, + // so that we don't move the map's center slightly before the quickzoom is started (see #14227) + gesturesManager.getMoveGestureDetector().setEnabled(false); } if (motionEvent.getActionMasked() == MotionEvent.ACTION_UP) { + // re-enabled the move detector + gesturesManager.getMoveGestureDetector().setEnabled(true); + if (!uiSettings.isZoomGesturesEnabled() || !uiSettings.isDoubleTapGesturesEnabled() || !executeDoubleTap) { return false; } PointF zoomFocalPoint; // Single finger double tap - if (focalPoint != null) { + if (constantFocalPoint != null) { // User provided focal point - zoomFocalPoint = focalPoint; + zoomFocalPoint = constantFocalPoint; } else { // Zoom in on gesture zoomFocalPoint = new PointF(motionEvent.getX(), motionEvent.getY()); @@ -468,9 +460,6 @@ final class MapGestureDetector { private final class ScaleGestureListener extends StandardScaleGestureDetector.SimpleStandardOnScaleGestureListener { private final float minimumVelocity; - - @Nullable - private PointF scaleFocalPoint; private boolean quickZoom; ScaleGestureListener(float minimumVelocity) { @@ -496,8 +485,6 @@ final class MapGestureDetector { if (!uiSettings.isQuickZoomGesturesEnabled()) { return false; } - // when quickzoom, disable move gesture - gesturesManager.getMoveGestureDetector().setEnabled(false); } cancelTransitionsIfRequired(); @@ -510,10 +497,7 @@ final class MapGestureDetector { ); } - // setting focalPoint in #onScaleBegin() as well, because #onScale() might not get called before #onScaleEnd() - setScaleFocalPoint(detector); - - sendTelemetryEvent(TelemetryConstants.PINCH, scaleFocalPoint); + sendTelemetryEvent(TelemetryConstants.PINCH, getScaleFocalPoint(detector)); notifyOnScaleBeginListeners(detector); @@ -525,11 +509,10 @@ final class MapGestureDetector { // dispatching camera start event only when the movement actually occurred cameraChangeDispatcher.onCameraMoveStarted(CameraChangeDispatcher.REASON_API_GESTURE); - setScaleFocalPoint(detector); - float scaleFactor = detector.getScaleFactor(); double zoomBy = getNewZoom(scaleFactor, quickZoom); - transform.zoomBy(zoomBy, scaleFocalPoint); + PointF focalPoint = getScaleFocalPoint(detector); + transform.zoomBy(zoomBy, focalPoint); notifyOnScaleListeners(detector); @@ -538,11 +521,6 @@ final class MapGestureDetector { @Override public void onScaleEnd(@NonNull StandardScaleGestureDetector detector, float velocityX, float velocityY) { - if (quickZoom) { - //if quickzoom, re-enabling move gesture detector - gesturesManager.getMoveGestureDetector().setEnabled(true); - } - if (uiSettings.isIncreaseRotateThresholdWhenScaling()) { // resetting default angle threshold gesturesManager.getRotateGestureDetector().setAngleThreshold( @@ -562,21 +540,23 @@ final class MapGestureDetector { double zoomAddition = calculateScale(velocityXY, detector.isScalingOut()); double currentZoom = transform.getRawZoom(); + PointF focalPoint = getScaleFocalPoint(detector); long animationTime = (long) (Math.abs(zoomAddition) * 1000 / 4); - scaleAnimator = createScaleAnimator(currentZoom, zoomAddition, scaleFocalPoint, animationTime); + scaleAnimator = createScaleAnimator(currentZoom, zoomAddition, focalPoint, animationTime); scheduleAnimator(scaleAnimator); } - private void setScaleFocalPoint(@NonNull StandardScaleGestureDetector detector) { - if (focalPoint != null) { + @NonNull + private PointF getScaleFocalPoint(@NonNull StandardScaleGestureDetector detector) { + if (constantFocalPoint != null) { // around user provided focal point - scaleFocalPoint = focalPoint; + return constantFocalPoint; } else if (quickZoom) { // around center - scaleFocalPoint = new PointF(uiSettings.getWidth() / 2, uiSettings.getHeight() / 2); + return new PointF(uiSettings.getWidth() / 2, uiSettings.getHeight() / 2); } else { // around gesture - scaleFocalPoint = detector.getFocalPoint(); + return detector.getFocalPoint(); } } @@ -603,14 +583,12 @@ final class MapGestureDetector { } private final class RotateGestureListener extends RotateGestureDetector.SimpleOnRotateGestureListener { - @Nullable - private PointF rotateFocalPoint; private final float minimumScaleSpanWhenRotating; private final float minimumAngularVelocity; private final float defaultSpanSinceStartThreshold; - public RotateGestureListener(float minimumScaleSpanWhenRotating, float minimumAngularVelocity, - float defaultSpanSinceStartThreshold) { + RotateGestureListener(float minimumScaleSpanWhenRotating, float minimumAngularVelocity, + float defaultSpanSinceStartThreshold) { this.minimumScaleSpanWhenRotating = minimumScaleSpanWhenRotating; this.minimumAngularVelocity = minimumAngularVelocity; this.defaultSpanSinceStartThreshold = defaultSpanSinceStartThreshold; @@ -631,10 +609,7 @@ final class MapGestureDetector { gesturesManager.getStandardScaleGestureDetector().interrupt(); } - // setting in #onRotateBegin() as well, because #onRotate() might not get called before #onRotateEnd() - setRotateFocalPoint(detector); - - sendTelemetryEvent(TelemetryConstants.ROTATION, rotateFocalPoint); + sendTelemetryEvent(TelemetryConstants.ROTATION, getRotateFocalPoint(detector)); notifyOnRotateBeginListeners(detector); @@ -647,13 +622,12 @@ final class MapGestureDetector { // dispatching camera start event only when the movement actually occurred cameraChangeDispatcher.onCameraMoveStarted(CameraChangeDispatcher.REASON_API_GESTURE); - setRotateFocalPoint(detector); - // Calculate map bearing value double bearing = transform.getRawBearing() + rotationDegreesSinceLast; // Rotate the map - transform.setBearing(bearing, rotateFocalPoint.x, rotateFocalPoint.y); + PointF focalPoint = getRotateFocalPoint(detector); + transform.setBearing(bearing, focalPoint.x, focalPoint.y); notifyOnRotateListeners(detector); @@ -687,21 +661,24 @@ final class MapGestureDetector { angularVelocity = -angularVelocity; } - rotateAnimator = createRotateAnimator(angularVelocity, animationTime); + PointF focalPoint = getRotateFocalPoint(detector); + rotateAnimator = createRotateAnimator(angularVelocity, animationTime, focalPoint); scheduleAnimator(rotateAnimator); } - private void setRotateFocalPoint(@NonNull RotateGestureDetector detector) { - if (focalPoint != null) { + @NonNull + private PointF getRotateFocalPoint(@NonNull RotateGestureDetector detector) { + if (constantFocalPoint != null) { // User provided focal point - rotateFocalPoint = focalPoint; + return constantFocalPoint; } else { // around gesture - rotateFocalPoint = detector.getFocalPoint(); + return detector.getFocalPoint(); } } - private Animator createRotateAnimator(float angularVelocity, long animationTime) { + private Animator createRotateAnimator(float angularVelocity, long animationTime, + @NonNull final PointF animationFocalPoint) { ValueAnimator animator = ValueAnimator.ofFloat(angularVelocity, 0f); animator.setDuration(animationTime); animator.setInterpolator(new DecelerateInterpolator()); @@ -710,7 +687,7 @@ final class MapGestureDetector { public void onAnimationUpdate(@NonNull ValueAnimator animation) { transform.setBearing( transform.getRawBearing() + (float) animation.getAnimatedValue(), - rotateFocalPoint.x, rotateFocalPoint.y, + animationFocalPoint.x, animationFocalPoint.y, 0L ); } @@ -802,9 +779,9 @@ final class MapGestureDetector { PointF zoomFocalPoint; // Single finger double tap - if (focalPoint != null) { + if (constantFocalPoint != null) { // User provided focal point - zoomFocalPoint = focalPoint; + zoomFocalPoint = constantFocalPoint; } else { // Zoom in on gesture zoomFocalPoint = detector.getFocalPoint(); |