diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2016-06-10 18:38:22 +0200 |
---|---|---|
committer | Tobrun <tobrun.van.nuland@gmail.com> | 2016-06-17 16:06:37 +0200 |
commit | 8e9b158e7347c00465d99f30f1220dff6c375178 (patch) | |
tree | 15e35ba6b95573cc3944e0be6d3ec7bd8cbacc0c /platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp | |
parent | fa0e3df981a6b6cbe9ae64564f3985a6fe34f5c7 (diff) | |
download | qtlocation-mapboxgl-8e9b158e7347c00465d99f30f1220dff6c375178.tar.gz |
[android] #5299 - Added more cars to the animation example to see what impact it has
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/AnimatedMarkerActivity.java | 84 |
1 files changed, 60 insertions, 24 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/AnimatedMarkerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/AnimatedMarkerActivity.java index 2842188723..76e662d6a7 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/AnimatedMarkerActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/AnimatedMarkerActivity.java @@ -9,7 +9,6 @@ import android.support.annotation.NonNull; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.animation.AccelerateDecelerateInterpolator; @@ -17,6 +16,7 @@ import android.view.animation.AccelerateDecelerateInterpolator; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.IconFactory; import com.mapbox.mapboxsdk.annotations.Marker; +import com.mapbox.mapboxsdk.annotations.MarkerOptions; import com.mapbox.mapboxsdk.annotations.MarkerViewOptions; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.geometry.LatLng; @@ -64,8 +64,13 @@ public class AnimatedMarkerActivity extends AppCompatActivity { setupMap(); addPassenger(); - addCar(); - animateMarker(carMarker, carMarker.getPosition(), passengerMarker.getPosition()); + addMainCar(); + animateMoveToPassenger(carMarker); + + for (int i = 0; i < 10; i++) { + addRandomCar(); + } + } }); } @@ -92,15 +97,11 @@ public class AnimatedMarkerActivity extends AppCompatActivity { } } - private void addCar() { + private void addMainCar() { LatLng randomLatLng = getLatLngInBounds(); if (carMarker == null) { - Icon icon = IconFactory.getInstance(AnimatedMarkerActivity.this) - .fromResource(R.drawable.ic_directions_car_black_24dp); - carMarker = mMapboxMap.addMarker(new MarkerViewOptions() - .position(randomLatLng) - .icon(icon)); + carMarker = createCarMarker(randomLatLng); } else { carMarker.setPosition(randomLatLng); } @@ -109,35 +110,70 @@ public class AnimatedMarkerActivity extends AppCompatActivity { mMapboxMap.selectMarker(carMarker); } - private LatLng getLatLngInBounds() { - LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds; - Random generator = new Random(); - double randomLat = bounds.getLatSouth() + generator.nextDouble() * (bounds.getLatNorth() - bounds.getLatSouth()); - double randomLon = bounds.getLonWest() + generator.nextDouble() * (bounds.getLonEast() - bounds.getLonWest()); - return new LatLng(randomLat, randomLon); + private void animateMoveToPassenger(final Marker car) { + + ValueAnimator animator = animateMoveMarker(car, passengerMarker.getPosition()); + animator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + addPassenger(); + animateMoveToPassenger(car); + } + }); } - private void animateMarker(final Marker marker, LatLng from, LatLng to) { + protected void addRandomCar() { + Marker car = createCarMarker(getLatLngInBounds()); + randomlyMoveMarker(car); + } - final ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position", new LatLngEvaluator(), from, to); - markerAnimator.setDuration((long) (10 * from.distanceTo(to))); - markerAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); + private void randomlyMoveMarker(final Marker marker) { + ValueAnimator animator = animateMoveMarker(marker, getLatLngInBounds()); //Add listener to restart animation on end - markerAnimator.addListener(new AnimatorListenerAdapter() { + animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { - Log.d(LOG_TAG, "Animation ended."); - - addPassenger(); - animateMarker(carMarker, carMarker.getPosition(), passengerMarker.getPosition()); + randomlyMoveMarker(marker); } }); + } + + private ValueAnimator animateMoveMarker(final Marker marker, LatLng to) { + final ValueAnimator markerAnimator = ObjectAnimator.ofObject(marker, "position", new LatLngEvaluator(), marker.getPosition(), to); + markerAnimator.setDuration((long) (10 * marker.getPosition().distanceTo(to))); + markerAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); // Start markerAnimator.start(); + + return markerAnimator; + } + + private Marker createCarMarker(LatLng start) { + Icon icon = IconFactory.getInstance(AnimatedMarkerActivity.this) + .fromResource(R.drawable.ic_directions_car_black_24dp); + + //View Markers + return mMapboxMap.addMarker(new MarkerViewOptions() + .position(start) + .icon(icon)); + + //GL Markers +// return mMapboxMap.addMarker(new MarkerOptions() +// .position(start) +// .icon(icon)); + + } + + private LatLng getLatLngInBounds() { + LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds; + Random generator = new Random(); + double randomLat = bounds.getLatSouth() + generator.nextDouble() * (bounds.getLatNorth() - bounds.getLatSouth()); + double randomLon = bounds.getLonWest() + generator.nextDouble() * (bounds.getLonEast() - bounds.getLonWest()); + return new LatLng(randomLat, randomLon); } @Override |