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 disable smooth animation of compass and accuracy …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
LukasPaczos committed Dec 14, 2018
1 parent e86d1b2 commit 9064c8e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.mapbox.mapboxsdk.location;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.location.Location;
import android.os.SystemClock;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -46,9 +45,13 @@ final class LocationAnimatorCoordinator {
private float previousCompassBearing = -1;
private long locationUpdateTimestamp = -1;
private float durationMultiplier;
private final MapboxAnimatorSetProvider animatorSetProvider;
private boolean compassAnimationEnabled;
private boolean accuracyAnimationEnabled;

LocationAnimatorCoordinator(Projection projection) {
LocationAnimatorCoordinator(@NonNull Projection projection, @NonNull MapboxAnimatorSetProvider animatorSetProvider) {
this.projection = projection;
this.animatorSetProvider = animatorSetProvider;
}

void addLayerListener(MapboxAnimator.OnLayerAnimationsValuesChangeListener listener) {
Expand Down Expand Up @@ -103,7 +106,7 @@ void feedNewCompassBearing(float targetCompassBearing, @NonNull CameraPosition c
float previousCameraBearing = (float) currentCameraPosition.bearing;

updateCompassAnimators(targetCompassBearing, previousLayerBearing, previousCameraBearing);
playCompassAnimators(COMPASS_UPDATE_RATE_MS);
playCompassAnimators(compassAnimationEnabled ? COMPASS_UPDATE_RATE_MS : 0);

previousCompassBearing = targetCompassBearing;
}
Expand All @@ -115,7 +118,7 @@ void feedNewAccuracyRadius(float targetAccuracyRadius, boolean noAnimation) {

float previousAccuracyRadius = getPreviousAccuracyRadius();
updateAccuracyAnimators(targetAccuracyRadius, previousAccuracyRadius);
playAccuracyAnimator(noAnimation ? 0 : ACCURACY_RADIUS_ANIMATION_DURATION);
playAccuracyAnimator(noAnimation || !accuracyAnimationEnabled ? 0 : ACCURACY_RADIUS_ANIMATION_DURATION);

this.previousAccuracyRadius = targetAccuracyRadius;
}
Expand Down Expand Up @@ -255,27 +258,20 @@ private void playLocationAnimators(long duration) {
locationAnimators.add(animatorArray.get(ANIMATOR_LAYER_GPS_BEARING));
locationAnimators.add(animatorArray.get(ANIMATOR_CAMERA_LATLNG));
locationAnimators.add(animatorArray.get(ANIMATOR_CAMERA_GPS_BEARING));
AnimatorSet locationAnimatorSet = new AnimatorSet();
locationAnimatorSet.playTogether(locationAnimators);
locationAnimatorSet.setInterpolator(new LinearInterpolator());
locationAnimatorSet.setDuration(duration);
locationAnimatorSet.start();
animatorSetProvider.startAnimation(locationAnimators, new LinearInterpolator(), duration);
}

private void playCompassAnimators(long duration) {
List<Animator> compassAnimators = new ArrayList<>();
compassAnimators.add(animatorArray.get(ANIMATOR_LAYER_COMPASS_BEARING));
compassAnimators.add(animatorArray.get(ANIMATOR_CAMERA_COMPASS_BEARING));
AnimatorSet compassAnimatorSet = new AnimatorSet();
compassAnimatorSet.playTogether(compassAnimators);
compassAnimatorSet.setDuration(duration);
compassAnimatorSet.start();
animatorSetProvider.startAnimation(compassAnimators, new LinearInterpolator(), duration);
}

private void playAccuracyAnimator(long duration) {
MapboxAnimator animator = animatorArray.get(ANIMATOR_LAYER_ACCURACY);
animator.setDuration(duration);
animator.start();
List<Animator> accuracyAnimators = new ArrayList<>();
accuracyAnimators.add(animatorArray.get(ANIMATOR_LAYER_ACCURACY));
animatorSetProvider.startAnimation(accuracyAnimators, new LinearInterpolator(), duration);
}

private void playZoomAnimator(long duration) {
Expand All @@ -294,11 +290,7 @@ private void playCameraLocationAnimators(long duration) {
List<Animator> locationAnimators = new ArrayList<>();
locationAnimators.add(animatorArray.get(ANIMATOR_CAMERA_LATLNG));
locationAnimators.add(animatorArray.get(ANIMATOR_CAMERA_GPS_BEARING));
AnimatorSet locationAnimatorSet = new AnimatorSet();
locationAnimatorSet.playTogether(locationAnimators);
locationAnimatorSet.setInterpolator(new FastOutSlowInInterpolator());
locationAnimatorSet.setDuration(duration);
locationAnimatorSet.start();
animatorSetProvider.startAnimation(locationAnimators, new FastOutSlowInInterpolator(), duration);
}

void resetAllCameraAnimations(@NonNull CameraPosition currentCameraPosition, boolean isGpsNorth) {
Expand Down Expand Up @@ -387,4 +379,12 @@ private void cancelAnimator(@MapboxAnimator.Type int animatorType) {
void setTrackingAnimationDurationMultiplier(float trackingAnimationDurationMultiplier) {
this.durationMultiplier = trackingAnimationDurationMultiplier;
}

void setCompassAnimationEnabled(boolean compassAnimationEnabled) {
this.compassAnimationEnabled = compassAnimationEnabled;
}

void setAccuracyAnimationEnabled(boolean accuracyAnimationEnabled) {
this.accuracyAnimationEnabled = accuracyAnimationEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ public void applyStyle(@NonNull final LocationComponentOptions options) {
staleStateManager.setEnabled(options.enableStaleState());
staleStateManager.setDelayTime(options.staleStateTimeout());
locationAnimatorCoordinator.setTrackingAnimationDurationMultiplier(options.trackingAnimationDurationMultiplier());
locationAnimatorCoordinator.setCompassAnimationEnabled(options.compassAnimationEnabled());
locationAnimatorCoordinator.setAccuracyAnimationEnabled(options.accuracyAnimationEnabled());
updateMapWithOptions(options);
}
}
Expand Down Expand Up @@ -1015,7 +1017,10 @@ private void initialize(@NonNull final Context context, @NonNull Style style,
locationCameraController = new LocationCameraController(
context, mapboxMap, cameraTrackingChangedListener, options, onCameraMoveInvalidateListener);

locationAnimatorCoordinator = new LocationAnimatorCoordinator(mapboxMap.getProjection());
locationAnimatorCoordinator = new LocationAnimatorCoordinator(
mapboxMap.getProjection(),
MapboxAnimatorSetProvider.getInstance()
);
locationAnimatorCoordinator.addLayerListener(locationLayerController);
locationAnimatorCoordinator.addCameraListener(locationCameraController);
locationAnimatorCoordinator.setTrackingAnimationDurationMultiplier(options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
final class LocationComponentConstants {

// Controls the compass update rate in milliseconds
static final int COMPASS_UPDATE_RATE_MS = 500;
static final long COMPASS_UPDATE_RATE_MS = 500;

// Sets the transition animation duration when switching camera modes.
static final long TRANSITION_ANIMATION_DURATION_MS = 750;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public class LocationComponentOptions implements Parcelable {
private float trackingMultiFingerMoveThreshold;
private String layerBelow;
private float trackingAnimationDurationMultiplier;
private boolean compassAnimationEnabled;
private boolean accuracyAnimationEnabled;

public LocationComponentOptions(
float accuracyAlpha,
Expand Down Expand Up @@ -141,7 +143,9 @@ public LocationComponentOptions(
float trackingInitialMoveThreshold,
float trackingMultiFingerMoveThreshold,
String layerBelow,
float trackingAnimationDurationMultiplier) {
float trackingAnimationDurationMultiplier,
boolean compassAnimationEnabled,
boolean accuracyAnimationEnabled) {
this.accuracyAlpha = accuracyAlpha;
this.accuracyColor = accuracyColor;
this.backgroundDrawableStale = backgroundDrawableStale;
Expand Down Expand Up @@ -175,6 +179,8 @@ public LocationComponentOptions(
this.trackingMultiFingerMoveThreshold = trackingMultiFingerMoveThreshold;
this.layerBelow = layerBelow;
this.trackingAnimationDurationMultiplier = trackingAnimationDurationMultiplier;
this.compassAnimationEnabled = compassAnimationEnabled;
this.accuracyAnimationEnabled = accuracyAnimationEnabled;
}

/**
Expand Down Expand Up @@ -281,6 +287,14 @@ public static LocationComponentOptions createFromAttributes(@NonNull Context con
);
builder.trackingAnimationDurationMultiplier(trackingAnimationDurationMultiplier);

builder.compassAnimationEnabled = typedArray.getBoolean(
R.styleable.mapbox_LocationComponent_mapbox_compassAnimationEnabled, true
);

builder.accuracyAnimationEnabled = typedArray.getBoolean(
R.styleable.mapbox_LocationComponent_mapbox_accuracyAnimationEnabled, true
);

typedArray.recycle();

return builder.build();
Expand Down Expand Up @@ -347,7 +361,7 @@ public int backgroundDrawableStale() {

/**
* String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will use this image in place of the provided or default mapbox_foregroundDrawableStale.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand All @@ -374,7 +388,7 @@ public int foregroundDrawableStale() {

/**
* String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_foregroundDrawableStale.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand All @@ -401,7 +415,7 @@ public int gpsDrawable() {

/**
* String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_gpsDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand All @@ -428,7 +442,7 @@ public int foregroundDrawable() {

/**
* String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_foregroundDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand All @@ -455,7 +469,7 @@ public int backgroundDrawable() {

/**
* String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_backgroundDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand All @@ -482,7 +496,7 @@ public int bearingDrawable() {

/**
* String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_bearingDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -684,6 +698,25 @@ public float trackingAnimationDurationMultiplier() {
return trackingAnimationDurationMultiplier;
}

/**
* Enable or disable smooth animation of compass values for {@link com.mapbox.mapboxsdk.location.modes.CameraMode}
* and {@link com.mapbox.mapboxsdk.location.modes.RenderMode}.
*
* @return whether smooth compass animation is enabled
*/
public boolean compassAnimationEnabled() {
return compassAnimationEnabled;
}

/**
* Enable or disable smooth animation of the accuracy circle around the user's position.
*
* @return whether smooth animation of the accuracy circle is enabled
*/
public boolean accuracyAnimationEnabled() {
return accuracyAnimationEnabled;
}

@NonNull
@Override
public String toString() {
Expand Down Expand Up @@ -836,6 +869,10 @@ public int hashCode() {
h$ ^= Float.floatToIntBits(trackingMultiFingerMoveThreshold);
h$ *= 1000003;
h$ ^= Float.floatToIntBits(trackingAnimationDurationMultiplier);
h$ *= 1000003;
h$ ^= compassAnimationEnabled ? 1231 : 1237;
h$ *= 1000003;
h$ ^= accuracyAnimationEnabled ? 1231 : 1237;
return h$;
}

Expand Down Expand Up @@ -873,7 +910,9 @@ public LocationComponentOptions createFromParcel(Parcel in) {
in.readFloat(),
in.readFloat(),
in.readString(),
in.readFloat()
in.readFloat(),
in.readInt() == 1,
in.readInt() == 1
);
}

Expand Down Expand Up @@ -970,6 +1009,8 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeFloat(trackingMultiFingerMoveThreshold());
dest.writeString(layerBelow());
dest.writeFloat(trackingAnimationDurationMultiplier);
dest.writeInt(compassAnimationEnabled() ? 1 : 0);
dest.writeInt(accuracyAnimationEnabled() ? 1 : 0);
}

@Override
Expand Down Expand Up @@ -1045,6 +1086,8 @@ public LocationComponentOptions build() {
private Float trackingMultiFingerMoveThreshold;
private String layerBelow;
private Float trackingAnimationDurationMultiplier;
private Boolean compassAnimationEnabled;
private Boolean accuracyAnimationEnabled;

Builder() {
}
Expand Down Expand Up @@ -1080,6 +1123,8 @@ private Builder(LocationComponentOptions source) {
this.trackingMultiFingerMoveThreshold = source.trackingMultiFingerMoveThreshold();
this.layerBelow = source.layerBelow();
this.trackingAnimationDurationMultiplier = source.trackingAnimationDurationMultiplier();
this.compassAnimationEnabled = source.compassAnimationEnabled();
this.accuracyAnimationEnabled = source.accuracyAnimationEnabled();
}

/**
Expand Down Expand Up @@ -1124,7 +1169,7 @@ public LocationComponentOptions.Builder backgroundDrawableStale(int backgroundDr

/**
* Given a String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_backgroundDrawableStale.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -1155,7 +1200,7 @@ public LocationComponentOptions.Builder foregroundDrawableStale(int foregroundDr

/**
* Given a String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_foregroundDrawableStale.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -1186,7 +1231,7 @@ public LocationComponentOptions.Builder gpsDrawable(int gpsDrawable) {

/**
* Given a String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_gpsDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -1217,7 +1262,7 @@ public LocationComponentOptions.Builder foregroundDrawable(int foregroundDrawabl

/**
* Given a String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_foregroundDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -1248,7 +1293,7 @@ public LocationComponentOptions.Builder backgroundDrawable(int backgroundDrawabl

/**
* Given a String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_backgroundDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -1279,7 +1324,7 @@ public LocationComponentOptions.Builder bearingDrawable(int bearingDrawable) {

/**
* Given a String image name, identical to one used in
* the first parameter of {@link com.mapbox.mapboxsdk.maps.MapboxMap#addImage(String, Bitmap)}, the
* the first parameter of {@link com.mapbox.mapboxsdk.maps.Style.Builder#addImage(String, Bitmap)}, the
* component, will used this image in place of the provided or default mapbox_bearingDrawable.
* <p>
* A maki-icon name (example: "circle-15") may also be provided. These are images that can be loaded
Expand Down Expand Up @@ -1524,6 +1569,27 @@ public LocationComponentOptions.Builder trackingAnimationDurationMultiplier(
return this;
}

/**
* Enable or disable smooth animation of compass values for {@link com.mapbox.mapboxsdk.location.modes.CameraMode}
* and {@link com.mapbox.mapboxsdk.location.modes.RenderMode}.
*
* @return whether smooth compass animation is enabled
*/
public LocationComponentOptions.Builder compassAnimationEnabled(Boolean compassAnimationEnabled) {
this.compassAnimationEnabled = compassAnimationEnabled;
return this;
}

/**
* Enable or disable smooth animation of the accuracy circle around the user's position.
*
* @return whether smooth animation of the accuracy circle is enabled
*/
public Builder accuracyAnimationEnabled(Boolean accuracyAnimationEnabled) {
this.accuracyAnimationEnabled = accuracyAnimationEnabled;
return this;
}

@Nullable
LocationComponentOptions autoBuild() {
String missing = "";
Expand Down Expand Up @@ -1614,7 +1680,9 @@ LocationComponentOptions autoBuild() {
this.trackingInitialMoveThreshold,
this.trackingMultiFingerMoveThreshold,
this.layerBelow,
this.trackingAnimationDurationMultiplier);
this.trackingAnimationDurationMultiplier,
this.compassAnimationEnabled,
this.accuracyAnimationEnabled);
}
}
}
Loading

0 comments on commit 9064c8e

Please sign in to comment.