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
This commit is contained in:
Tobrun
2016-10-10 23:51:30 +02:00
committed by GitHub
parent 5891c7028d
commit e598bbfa7c
6 changed files with 213 additions and 33 deletions

View File

@@ -126,6 +126,12 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
*/
public T rotation(float rotation) {
this.rotation = rotation;
while (this.rotation > 360) {
this.rotation -= 360;
}
while (this.rotation < 0) {
this.rotation += 360;
}
return getThis();
}

View File

@@ -1,18 +1,12 @@
package com.mapbox.mapboxsdk.annotations;
import android.animation.AnimatorSet;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AnimationUtils;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.utils.AnimatorUtils;
/**
* MarkerView is an annotation that shows an View at a geographical location.
@@ -231,7 +225,10 @@ public class MarkerView extends Marker {
}
/**
* Set the rotation value of the MarkerView.
* Set the rotation value of the MarkerView in degrees.
* <p>
* Input will be limited to 0 - 360 degrees
* </p>
* <p>
* This will result in animating the rotation of the MarkerView using an rotation animator
* from current value to the provided parameter value.
@@ -240,9 +237,26 @@ public class MarkerView extends Marker {
* @param rotation the rotation value to animate to
*/
public void setRotation(float rotation) {
this.rotation = rotation;
// limit to 0 - 360 degrees
float newRotation = rotation;
while (newRotation > 360) {
newRotation -= 360;
}
while (newRotation < 0) {
newRotation += 360;
}
// calculate new direction
float diff = newRotation - this.rotation;
if (diff > 180.0f) {
diff -= 360.0f;
} else if (diff < -180.0f) {
diff += 360.f;
}
this.rotation = newRotation;
if (markerViewManager != null) {
markerViewManager.animateRotation(this, rotation);
markerViewManager.animateRotationBy(this, diff);
}
}
@@ -339,7 +353,7 @@ public class MarkerView extends Marker {
public void setMapboxMap(MapboxMap mapboxMap) {
super.setMapboxMap(mapboxMap);
if(isFlat()) {
if (isFlat()) {
// initial tilt value if MapboxMap is started with a tilt attribute
tiltValue = (float) mapboxMap.getCameraPosition().tilt;
}

View File

@@ -15,7 +15,6 @@ import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.utils.AnimatorUtils;
import java.util.ArrayList;
@@ -71,6 +70,19 @@ public class MarkerViewManager {
}
}
/**
* Animate a MarkerView with a given rotation.
*
* @param marker the MarkerView to rotate by
* @param rotation the rotation by value
*/
public void animateRotationBy(@NonNull MarkerView marker, float rotation) {
View convertView = markerViewMap.get(marker);
if (convertView != null) {
AnimatorUtils.rotateBy(convertView, rotation);
}
}
/**
* Animate a MarkerView to a given alpha value.
* <p>

View File

@@ -7,6 +7,7 @@ import android.animation.ObjectAnimator;
import android.support.annotation.AnimatorRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.view.View;
public class AnimatorUtils {
@@ -61,6 +62,17 @@ public class AnimatorUtils {
rotateAnimator.start();
}
public static void rotateBy(@NonNull final View view, float rotationBy) {
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
view.animate().rotationBy(rotationBy).setInterpolator(new FastOutSlowInInterpolator()).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setLayerType(View.LAYER_TYPE_NONE, null);
}
});
}
public static void alpha(@NonNull final View convertView, float alpha, @Nullable final OnAnimationEndListener listener) {
convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(convertView, View.ALPHA, convertView.getAlpha(), alpha);