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

Commit

Permalink
[android] #5299 - Simplify animation code a bit. Make duration depend…
Browse files Browse the repository at this point in the history
…ent on distance. Use non-linear interpolator.
  • Loading branch information
ivovandongen authored and tobrun committed Jun 17, 2016
1 parent 27da49c commit fa0e3df
Showing 1 changed file with 14 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.mapbox.mapboxsdk.testapp.activity.annotation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.os.Bundle;
import android.support.annotation.NonNull;
Expand All @@ -10,7 +12,7 @@
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.LinearInterpolator;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
Expand Down Expand Up @@ -116,24 +118,13 @@ private LatLng getLatLngInBounds() {
}

private void animateMarker(final Marker marker, LatLng from, LatLng to) {
ValueAnimator markerAnimator = ValueAnimator.ofObject(new LatLngEvaluator(), (Object[]) new LatLng[]{from, to});
markerAnimator.setDuration(5000);
markerAnimator.setInterpolator(new LinearInterpolator());

markerAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (marker != null) {
marker.setPosition((LatLng) animation.getAnimatedValue());
}
}
});
final ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position", new LatLngEvaluator(), from, to);
markerAnimator.setDuration((long) (10 * from.distanceTo(to)));
markerAnimator.setInterpolator(new AccelerateDecelerateInterpolator());

markerAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// Nothing
}
//Add listener to restart animation on end
markerAnimator.addListener(new AnimatorListenerAdapter() {

@Override
public void onAnimationEnd(Animator animation) {
Expand All @@ -143,15 +134,6 @@ public void onAnimationEnd(Animator animation) {
animateMarker(carMarker, carMarker.getPosition(), passengerMarker.getPosition());
}

@Override
public void onAnimationCancel(Animator animation) {
// Nothing
}

@Override
public void onAnimationRepeat(Animator animation) {
// Nothing
}
});

// Start
Expand Down Expand Up @@ -205,14 +187,17 @@ public void onLowMemory() {
mMapView.onLowMemory();
}

private class LatLngEvaluator implements TypeEvaluator<LatLng> {
/**
* Evaluator for LatLng pairs
*/
private static class LatLngEvaluator implements TypeEvaluator<LatLng> {

private LatLng mLatLng = new LatLng();

@Override
public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
mLatLng.setLatitude(startValue.getLatitude() + (endValue.getLatitude() - startValue.getLatitude()) * fraction);
mLatLng.setLongitude(startValue.getLongitude() + (endValue.getLongitude() - startValue.getLongitude()) * fraction);
mLatLng.setLatitude(startValue.getLatitude() + ((endValue.getLatitude() - startValue.getLatitude()) * fraction));
mLatLng.setLongitude(startValue.getLongitude() + ((endValue.getLongitude() - startValue.getLongitude()) * fraction));
return mLatLng;
}
}
Expand Down

0 comments on commit fa0e3df

Please sign in to comment.