From f62f1dd0b79ce271498d9a0b8dc8def1bc1ac65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Tue, 17 Apr 2018 12:25:40 +0200 Subject: [PATCH] [LLP] - added max animation duration check and transition animation duration --- .../locationlayer/LocationLayerAnimator.java | 39 ++++++++++--------- .../locationlayer/LocationLayerConstants.java | 9 ++--- .../locationlayer/camera/BearingAnimator.java | 3 +- .../locationlayer/camera/LatLngAnimator.java | 3 +- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java index eb68c97fb..9e220bb4e 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerAnimator.java @@ -17,6 +17,8 @@ import java.util.List; import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.COMPASS_UPDATE_RATE_MS; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.MAX_ANIMATION_DURATION_MS; +import static com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerConstants.TRANSITION_ANIMATION_DURATION_MS; final class LocationLayerAnimator { @@ -175,7 +177,7 @@ interface OnCameraAnimationsValuesChangeListener { void resetAllCameraAnimations(CameraPosition currentCameraPosition, boolean isGpsNorth) { resetCameraCompassAnimation(currentCameraPosition); resetCameraLocationAnimations(currentCameraPosition, isGpsNorth); - playCameraAnimators(); + playCameraAnimators(TRANSITION_ANIMATION_DURATION_MS); } void cancelAllAnimations() { @@ -223,9 +225,9 @@ private float getPreviousLayerCompassBearing() { private void updateLayerAnimators(LatLng previousLatLng, LatLng targetLatLng, float previousBearing, float targetBearing) { cancelLayerLocationAnimations(); - layerLatLngAnimator = new LatLngAnimator(previousLatLng, targetLatLng, 1000); + layerLatLngAnimator = new LatLngAnimator(previousLatLng, targetLatLng); float normalizedLayerBearing = Utils.shortestRotation(targetBearing, previousBearing); - layerGpsBearingAnimator = new BearingAnimator(previousBearing, normalizedLayerBearing, 1000); + layerGpsBearingAnimator = new BearingAnimator(previousBearing, normalizedLayerBearing); layerLatLngAnimator.addUpdateListener(layerLatLngUpdateListener); layerGpsBearingAnimator.addUpdateListener(layerGpsBearingUpdateListener); @@ -234,25 +236,28 @@ private void updateLayerAnimators(LatLng previousLatLng, LatLng targetLatLng, private void updateCameraAnimators(LatLng previousCameraLatLng, float previousCameraBearing, LatLng targetLatLng, float targetBearing) { cancelCameraLocationAnimations(); - cameraLatLngAnimator = new LatLngAnimator(previousCameraLatLng, targetLatLng, 1000); + cameraLatLngAnimator = new LatLngAnimator(previousCameraLatLng, targetLatLng); cameraLatLngAnimator.addUpdateListener(cameraLatLngUpdateListener); float normalizedCameraBearing = Utils.shortestRotation(targetBearing, previousCameraBearing); - cameraGpsBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing, 1000); + cameraGpsBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing); cameraGpsBearingAnimator.addUpdateListener(cameraGpsBearingUpdateListener); } private long getAnimationDuration() { - float previousUpdateTimeStamp = locationUpdateTimestamp; + long previousUpdateTimeStamp = locationUpdateTimestamp; locationUpdateTimestamp = SystemClock.elapsedRealtime(); - int animationDuration; + long animationDuration; if (previousUpdateTimeStamp == 0) { animationDuration = 0; } else { - animationDuration = (int) ((locationUpdateTimestamp - previousUpdateTimeStamp) * 1.1f) - /*make animation slightly longer*/; + animationDuration = (long) ((locationUpdateTimestamp - previousUpdateTimeStamp) * 1.1f) + /*make animation slightly longer*/; } + + animationDuration = Math.min(animationDuration, MAX_ANIMATION_DURATION_MS); + return animationDuration; } @@ -276,13 +281,14 @@ private void playAllLocationAnimators(long duration) { locationAnimatorSet.start(); } - private void playCameraAnimators() { + private void playCameraAnimators(long duration) { List locationAnimators = new ArrayList<>(); locationAnimators.add(cameraLatLngAnimator); locationAnimators.add(cameraGpsBearingAnimator); AnimatorSet locationAnimatorSet = new AnimatorSet(); locationAnimatorSet.playTogether(locationAnimators); locationAnimatorSet.setInterpolator(new LinearInterpolator()); + locationAnimatorSet.setDuration(duration); locationAnimatorSet.start(); } @@ -290,12 +296,12 @@ private void updateCompassAnimators(float targetCompassBearing, float previousLa float previousCameraBearing) { cancelLayerCompassAnimations(); float normalizedLayerBearing = Utils.shortestRotation(targetCompassBearing, previousLayerBearing); - layerCompassBearingAnimator = new BearingAnimator(previousLayerBearing, normalizedLayerBearing, 1000); + layerCompassBearingAnimator = new BearingAnimator(previousLayerBearing, normalizedLayerBearing); layerCompassBearingAnimator.addUpdateListener(layerCompassBearingUpdateListener); cancelCameraCompassAnimations(); float normalizedCameraBearing = Utils.shortestRotation(targetCompassBearing, previousCameraBearing); - cameraCompassBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing, 1000); + cameraCompassBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing); cameraCompassBearingAnimator.addUpdateListener(cameraCompassBearingUpdateListener); } @@ -356,11 +362,10 @@ private void resetCameraLatLngAnimation(CameraPosition currentCameraPosition) { if (cameraLatLngAnimator == null) { return; } - long duration = cameraLatLngAnimator.getDuration(); cancelCameraLatLngAnimations(); LatLng currentTarget = cameraLatLngAnimator.getTarget(); LatLng previousCameraTarget = currentCameraPosition.target; - cameraLatLngAnimator = new LatLngAnimator(previousCameraTarget, currentTarget, duration); + cameraLatLngAnimator = new LatLngAnimator(previousCameraTarget, currentTarget); cameraLatLngAnimator.addUpdateListener(cameraLatLngUpdateListener); } @@ -375,13 +380,12 @@ private void resetCameraGpsBearingAnimation(CameraPosition currentCameraPosition if (cameraGpsBearingAnimator == null) { return; } - long duration = cameraLatLngAnimator.getDuration(); cancelCameraGpsBearingAnimations(); float currentTargetBearing = cameraGpsBearingAnimator.getTargetBearing(); currentTargetBearing = checkGpsNorth(isGpsNorth, currentTargetBearing); float previousCameraBearing = (float) currentCameraPosition.bearing; float normalizedCameraBearing = Utils.shortestRotation(currentTargetBearing, previousCameraBearing); - cameraGpsBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing, duration); + cameraGpsBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing); cameraGpsBearingAnimator.addUpdateListener(cameraGpsBearingUpdateListener); } @@ -396,12 +400,11 @@ private void resetCameraCompassAnimation(CameraPosition currentCameraPosition) { if (cameraCompassBearingAnimator == null || cameraLatLngAnimator == null) { return; } - long duration = cameraLatLngAnimator.getDuration(); cancelCameraCompassAnimations(); float currentTargetBearing = cameraCompassBearingAnimator.getTargetBearing(); float previousCameraBearing = (float) currentCameraPosition.bearing; float normalizedCameraBearing = Utils.shortestRotation(currentTargetBearing, previousCameraBearing); - cameraCompassBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing, duration); + cameraCompassBearingAnimator = new BearingAnimator(previousCameraBearing, normalizedCameraBearing); cameraCompassBearingAnimator.addUpdateListener(cameraCompassBearingUpdateListener); } } diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java index 840a0c92b..ab1a07b43 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/LocationLayerConstants.java @@ -10,12 +10,11 @@ final class LocationLayerConstants { // Controls the compass update rate in milliseconds static final int COMPASS_UPDATE_RATE_MS = 500; - // Sets the animation time when moving the user location icon from the previous location to the - // updated. If LinearAnimator's enabled, this values ignored. - static final int LOCATION_UPDATE_DELAY_MS = 500; + // Sets the transition animation duration when switching camera modes. + static final long TRANSITION_ANIMATION_DURATION_MS = 750; - // Sets the max allowed time for the location icon animation from one latlng to another. - static final long MIN_ANIMATION_DURATION_MS = 1500; + // Sets the max allowed time for the location icon animation from one LatLng to another. + static final long MAX_ANIMATION_DURATION_MS = 2000; // Sources static final String LOCATION_SOURCE = "mapbox-location-source"; diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java index 7e73b856e..53ec82454 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/BearingAnimator.java @@ -7,8 +7,7 @@ public class BearingAnimator extends ValueAnimator { private float targetBearing; - public BearingAnimator(float previous, float target, long duration) { - setDuration(duration); + public BearingAnimator(float previous, float target) { setEvaluator(new FloatEvaluator()); setFloatValues(previous, target); this.targetBearing = target; diff --git a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java index 829c71121..243d9890d 100644 --- a/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java +++ b/plugin-locationlayer/src/main/java/com/mapbox/mapboxsdk/plugins/locationlayer/camera/LatLngAnimator.java @@ -10,8 +10,7 @@ public class LatLngAnimator extends ValueAnimator { private LatLng target; - public LatLngAnimator(@NonNull LatLng previous, @NonNull LatLng target, long duration) { - setDuration(duration); + public LatLngAnimator(@NonNull LatLng previous, @NonNull LatLng target) { setObjectValues(previous, target); setEvaluator(new LatLngEvaluator()); this.target = target;