From b008ec41c08643762e1115ef8447776b3746f723 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Thu, 12 Jan 2017 21:25:40 +0100 Subject: [PATCH] [android] - improve fling gesture by calculating animation time from velocity, ignore small fling gestures. (#7676) --- .../mapboxsdk/constants/MapboxConstants.java | 12 +++++++++++- .../com/mapbox/mapboxsdk/maps/MapView.java | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java index 1b76b3d2a33..f45a459dca0 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java @@ -8,7 +8,7 @@ */ public class MapboxConstants { - /** + /** * Default Locale for data processing (ex: String.toLowerCase(MAPBOX_LOCALE, "foo")) */ public static final Locale MAPBOX_LOCALE = Locale.US; @@ -141,4 +141,14 @@ public class MapboxConstants { public static final String MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_ENABLED = "mapboxTelemetryEnabled"; public static final String MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_URL = "mapboxTelemetryStagingUrl"; public static final String MAPBOX_SHARED_PREFERENCE_KEY_TELEMETRY_STAGING_ACCESS_TOKEN = "mapboxTelemetryStagingAccessToken"; + + /** + * Animation time of a fling gesture + */ + public static final long ANIMATION_DURATION_FLING_BASE = ANIMATION_DURATION_SHORT; + + /** + * Velocity threshold for a fling gesture + */ + public static final long VELOCITY_THRESHOLD_IGNORE_FLING = 1000; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index 2c816b81b60..b3ea9ef471e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -1986,17 +1986,28 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve return false; } + // calculate velocity vector for xy dimensions, independent from screen size + double velocityXY = Math.hypot(velocityX / screenDensity, velocityY / screenDensity); + if (velocityXY < MapboxConstants.VELOCITY_THRESHOLD_IGNORE_FLING) { + // ignore short flings, these can occur when other gestures just have finished executing + return false; + } + resetTrackingModesIfRequired(true, false); + // tilt results in a bigger translation, limiting input for #5281 double tilt = getTilt(); - double limitFactor = 2 + ((tilt != 0) ? (tilt / 10) : 0); - double offsetX = velocityX / limitFactor / screenDensity; - double offsetY = velocityY / limitFactor / screenDensity; + double tiltFactor = 1 + ((tilt != 0) ? (tilt / 10) : 0); /* 1 -> 7 */ + double offsetX = velocityX / tiltFactor / screenDensity; + double offsetY = velocityY / tiltFactor / screenDensity; + + // calculate animation time + long animationTime = (long) (velocityXY / 7 / tiltFactor + MapboxConstants.ANIMATION_DURATION_FLING_BASE); // Cancel any animation cancelTransitions(); - nativeMapView.moveBy(offsetX, offsetY, MapboxConstants.ANIMATION_DURATION_FLING); + nativeMapView.moveBy(offsetX, offsetY, animationTime); MapboxMap.OnFlingListener listener = mapboxMap.getOnFlingListener(); if (listener != null) {