Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] option to change the zoom rate
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed May 28, 2019
1 parent b9c46c0 commit 865f378
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public class MapboxConstants {
*/
public static final float MAXIMUM_SCALE_FACTOR_CLAMP = 0.15f;

/**
* Zoom value multiplier for scale gestures.
*/
public static final float ZOOM_RATE = 0.65f;

/**
* Fragment Argument Key for MapboxMapOptions
*/
Expand All @@ -146,6 +151,7 @@ public class MapboxConstants {
public static final String STATE_TILT_ENABLED = "mapbox_tiltEnabled";
public static final String STATE_DOUBLE_TAP_ENABLED = "mapbox_doubleTapEnabled";
public static final String STATE_QUICK_ZOOM_ENABLED = "mapbox_quickZoom";
public static final String STATE_ZOOM_RATE = "mapbox_zoomRate";
public static final String STATE_DEBUG_ACTIVE = "mapbox_debugActive";
public static final String STATE_COMPASS_ENABLED = "mapbox_compassEnabled";
public static final String STATE_COMPASS_GRAVITY = "mapbox_compassGravity";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;

import com.mapbox.android.gestures.AndroidGesturesManager;
import com.mapbox.android.gestures.Constants;
import com.mapbox.android.gestures.ShoveGestureDetector;
import com.mapbox.android.gestures.StandardGestureDetector;
import com.mapbox.android.gestures.MultiFingerTapGestureDetector;
import com.mapbox.android.gestures.MoveGestureDetector;
import com.mapbox.android.gestures.MultiFingerTapGestureDetector;
import com.mapbox.android.gestures.RotateGestureDetector;
import com.mapbox.android.gestures.ShoveGestureDetector;
import com.mapbox.android.gestures.StandardGestureDetector;
import com.mapbox.android.gestures.StandardScaleGestureDetector;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
Expand All @@ -29,6 +30,7 @@
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.mapbox.mapboxsdk.constants.MapboxConstants.ZOOM_RATE;
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener.REASON_API_ANIMATION;
import static com.mapbox.mapboxsdk.maps.MapboxMap.OnCameraMoveStartedListener.REASON_API_GESTURE;

Expand Down Expand Up @@ -558,7 +560,7 @@ private double calculateScale(double velocityXY, boolean isScalingOut) {
}

private double getNewZoom(float scaleFactor, boolean quickZoom) {
double zoomBy = (Math.log(scaleFactor) / Math.log(Math.PI / 2)) * 0.65;
double zoomBy = (Math.log(scaleFactor) / Math.log(Math.PI / 2)) * ZOOM_RATE * uiSettings.getZoomRate();
if (quickZoom) {
// clamp scale factors we feed to core #7514
boolean negative = zoomBy < 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.Px;
Expand Down Expand Up @@ -68,6 +69,8 @@ public final class UiSettings {
private boolean increaseRotateThresholdWhenScaling = true;
private boolean increaseScaleThresholdWhenRotating = true;

private float zoomRate = 1.0f;

private boolean deselectMarkersOnTap = true;

@Nullable
Expand Down Expand Up @@ -131,6 +134,7 @@ private void saveGestures(Bundle outState) {
outState.putBoolean(MapboxConstants.STATE_INCREASE_ROTATE_THRESHOLD, isIncreaseRotateThresholdWhenScaling());
outState.putBoolean(MapboxConstants.STATE_INCREASE_SCALE_THRESHOLD, isIncreaseScaleThresholdWhenRotating());
outState.putBoolean(MapboxConstants.STATE_QUICK_ZOOM_ENABLED, isQuickZoomGesturesEnabled());
outState.putFloat(MapboxConstants.STATE_ZOOM_RATE, getZoomRate());
}

private void restoreGestures(Bundle savedInstanceState) {
Expand All @@ -147,6 +151,7 @@ private void restoreGestures(Bundle savedInstanceState) {
setIncreaseScaleThresholdWhenRotating(
savedInstanceState.getBoolean(MapboxConstants.STATE_INCREASE_SCALE_THRESHOLD));
setQuickZoomGesturesEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_QUICK_ZOOM_ENABLED));
setZoomRate(savedInstanceState.getFloat(MapboxConstants.STATE_ZOOM_RATE, 1.0f));
}

private void initialiseCompass(MapboxMapOptions options, @NonNull Resources resources) {
Expand Down Expand Up @@ -771,6 +776,27 @@ public void setQuickZoomGesturesEnabled(boolean quickZoomGesturesEnabled) {
this.quickZoomGesturesEnabled = quickZoomGesturesEnabled;
}

/**
* Returns zoom gesture rate, including pinch to zoom and quick scale.
*
* @return The zoom rate.
*/
public float getZoomRate() {
return zoomRate;
}

/**
* Sets zoom gesture rate, including pinch to zoom and quick scale.
* <p>
* Default value is 1.0f.
* </p>
*
* @param zoomRate The zoom rate.
*/
public void setZoomRate(@FloatRange(from = 0f) float zoomRate) {
this.zoomRate = zoomRate;
}

private void restoreDeselectMarkersOnTap(Bundle savedInstanceState) {
setDeselectMarkersOnTap(savedInstanceState.getBoolean(MapboxConstants.STATE_DESELECT_MARKER_ON_TAP));
}
Expand Down Expand Up @@ -961,7 +987,7 @@ public void setAllGesturesEnabled(boolean enabled) {
*/
public boolean areAllGesturesEnabled() {
return rotateGesturesEnabled && tiltGesturesEnabled && zoomGesturesEnabled
&& scrollGesturesEnabled && doubleTapGesturesEnabled && quickZoomGesturesEnabled;
&& scrollGesturesEnabled && doubleTapGesturesEnabled && quickZoomGesturesEnabled;
}

private void saveFocalPoint(Bundle outState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,17 @@ public void testAreAllGesturesEnabledWithOneGestureDisabled() {
assertEquals("All gestures check should return false", false,
uiSettings.areAllGesturesEnabled());
}

@Test
public void testZoomRateDefaultValue() {
assertEquals("Default zoom rate should be 1.0f", 1.0f,
uiSettings.getZoomRate(), 0);
}

@Test
public void testZoomRate() {
uiSettings.setZoomRate(0.83f);
assertEquals("Zoom rate should be 0.83f", 0.83f,
uiSettings.getZoomRate(), 0);
}
}

0 comments on commit 865f378

Please sign in to comment.