From e598bbfa7c29ad3a384e157b504c4509f1fcdbb5 Mon Sep 17 00:00:00 2001 From: Tobrun Date: Mon, 10 Oct 2016 23:51:30 +0200 Subject: Rotate markerview (#6640) * [android] - MarkerView rotation bug, limit degrees from 0 to 360 while creating a MarkerView * update MarkerView#setRotation to limit input from 0 - 360 degrees, calculates difference in angle and animates rotation by a certain amount instead of animating to a value. Exposed new AnimatorUtils#animateRotationTo. Integrated into MarkerViewManager. Cleanup some unused imports. Updated javadoc. * updated MarkerViewActivity in test application to test for regressions. * added tests to validate changes to MarkerView#setRotate --- .../activity/annotation/MarkerViewActivity.java | 76 ++++++++++++++++------ 1 file changed, 56 insertions(+), 20 deletions(-) (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk') diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/MarkerViewActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/MarkerViewActivity.java index ccce727130..8d678794c4 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/MarkerViewActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/MarkerViewActivity.java @@ -41,24 +41,30 @@ import java.util.Random; public class MarkerViewActivity extends AppCompatActivity { + private static final LatLng[] LAT_LNGS = new LatLng[]{ + new LatLng(38.897424, -77.036508), + new LatLng(38.909698, -77.029642), + new LatLng(38.907227, -77.036530), + new LatLng(38.905607, -77.031916), + new LatLng(38.889441, -77.050134), + new LatLng(38.888000, -77.050000) //Slight overlap to show re-ordering on selection + }; + private MapboxMap mapboxMap; private MapView mapView; + // MarkerView location updates private MarkerView movingMarkerOne; private MarkerView movingMarkerTwo; private Random randomAnimator = new Random(); private Handler locationUpdateHandler = new Handler(); private Runnable moveMarkerRunnable = new MoveMarkerRunnable(); - private int rotation = 0; - private static final LatLng[] LAT_LNGS = new LatLng[]{ - new LatLng(38.897424, -77.036508), - new LatLng(38.909698, -77.029642), - new LatLng(38.907227, -77.036530), - new LatLng(38.905607, -77.031916), - new LatLng(38.889441, -77.050134), - new LatLng(38.888000, -77.050000) //Slight overlap to show re-ordering on selection - }; + // MarkerView rotate updates + private MarkerView rotateMarker; + private Handler rotateUpdateHandler = new Handler(); + private Runnable rotateMarkerRunnable = new RotateMarkerRunnable(); + private int rotation = 360; @Override protected void onCreate(Bundle savedInstanceState) { @@ -111,10 +117,13 @@ public class MarkerViewActivity extends AppCompatActivity { .position(new LatLng(38.902580, -77.050102)) ); - MarkerViewActivity.this.mapboxMap.addMarker(new TextMarkerViewOptions() + rotateMarker = MarkerViewActivity.this.mapboxMap.addMarker(new TextMarkerViewOptions() .text("A") + .rotation(rotation = 270) .position(new LatLng(38.889876, -77.008849)) ); + loopMarkerRotate(); + MarkerViewActivity.this.mapboxMap.addMarker(new TextMarkerViewOptions() .text("B") @@ -167,6 +176,10 @@ public class MarkerViewActivity extends AppCompatActivity { }); } + private void loopMarkerRotate() { + rotateUpdateHandler.postDelayed(rotateMarkerRunnable, 800); + } + @Override protected void onStart() { super.onStart(); @@ -183,27 +196,49 @@ public class MarkerViewActivity extends AppCompatActivity { locationUpdateHandler.removeCallbacks(moveMarkerRunnable); } + /** + * Updates the position of a Marker + */ private class MoveMarkerRunnable implements Runnable { @Override public void run() { int i = randomAnimator.nextInt(9); if (randomAnimator.nextInt() % 2 == 0) { movingMarkerOne.setPosition(CarLocation.CAR_0_LNGS[i]); - movingMarkerOne.setRotation(rotation = rotation + 45); } else { movingMarkerTwo.setPosition(CarLocation.CAR_1_LNGS[i]); - movingMarkerTwo.setRotation(rotation = rotation + 90); } loopMarkerMove(); } } + /** + * Updates the rotation of a Marker + */ + private class RotateMarkerRunnable implements Runnable { + + private final static int ROTATION_INCREASE_VALUE = 9; + + @Override + public void run() { + rotation -= ROTATION_INCREASE_VALUE; + if (rotation >= 0) { + rotation += 360; + } + rotateMarker.setRotation(rotation); + loopMarkerRotate(); + } + } + + /** + * Adapts a MarkerView to display an abbreviated name in a TextView and a flag in an ImageView. + */ private static class CountryAdapter extends MapboxMap.MarkerViewAdapter { private LayoutInflater inflater; private MapboxMap mapboxMap; - public CountryAdapter(@NonNull Context context, @NonNull MapboxMap mapboxMap) { + CountryAdapter(@NonNull Context context, @NonNull MapboxMap mapboxMap) { super(context); this.inflater = LayoutInflater.from(context); this.mapboxMap = mapboxMap; @@ -229,7 +264,7 @@ public class MarkerViewActivity extends AppCompatActivity { @Override public boolean onSelect( - @NonNull final CountryMarkerView marker, @NonNull final View convertView, boolean reselectionForViewReuse) { + @NonNull final CountryMarkerView marker, @NonNull final View convertView, boolean reselectionForViewReuse) { convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null); ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(convertView, View.ROTATION, 0, 360); rotateAnimator.setDuration(reselectionForViewReuse ? 0 : 350); @@ -268,13 +303,15 @@ public class MarkerViewActivity extends AppCompatActivity { } } - + /** + * Adapts a MarkerView to display text in a TextView. + */ private static class TextAdapter extends MapboxMap.MarkerViewAdapter { private LayoutInflater inflater; private MapboxMap mapboxMap; - public TextAdapter(@NonNull Context context, @NonNull MapboxMap mapboxMap) { + TextAdapter(@NonNull Context context, @NonNull MapboxMap mapboxMap) { super(context); this.inflater = LayoutInflater.from(context); this.mapboxMap = mapboxMap; @@ -298,7 +335,7 @@ public class MarkerViewActivity extends AppCompatActivity { @Override public boolean onSelect( - @NonNull final TextMarkerView marker, @NonNull final View convertView, boolean reselectionForViewReuse) { + @NonNull final TextMarkerView marker, @NonNull final View convertView, boolean reselectionForViewReuse) { animateGrow(marker, convertView, 0); // false indicates that we are calling selectMarker after our animation ourselves @@ -409,7 +446,7 @@ public class MarkerViewActivity extends AppCompatActivity { private static class CarLocation { - public static LatLng[] CAR_0_LNGS = new LatLng[]{ + static LatLng[] CAR_0_LNGS = new LatLng[]{ new LatLng(38.92334425495122, -77.0533673443786), new LatLng(38.9234737236897, -77.05389484528261), new LatLng(38.9257094658146, -76.98819752280579), @@ -421,7 +458,7 @@ public class MarkerViewActivity extends AppCompatActivity { new LatLng(38.862930274733635, -76.99647808241964) }; - public static LatLng[] CAR_1_LNGS = new LatLng[]{ + static LatLng[] CAR_1_LNGS = new LatLng[]{ new LatLng(38.94237975070426, -76.98324549005675), new LatLng(38.941520236084486, -76.98234257804742), new LatLng(38.85972219720714, -76.98955808483929), @@ -432,6 +469,5 @@ public class MarkerViewActivity extends AppCompatActivity { new LatLng(38.882869724926245, -77.02992539231113), new LatLng(38.9371988177896, -76.97786740676564) }; - } } -- cgit v1.2.1