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

Commit

Permalink
[android] - improve fling gesture by calculating animation time from …
Browse files Browse the repository at this point in the history
…velocity, ignore small fling gestures. (#7676)
  • Loading branch information
tobrun committed Jan 17, 2017
1 parent c257cb6 commit b008ec4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b008ec4

Please sign in to comment.