Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -276,26 +281,27 @@ private void playAllLocationAnimators(long duration) {
locationAnimatorSet.start();
}

private void playCameraAnimators() {
private void playCameraAnimators(long duration) {
List<Animator> 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();
}

private void updateCompassAnimators(float targetCompassBearing, float previousLayerBearing,
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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down