[android] Fix my location drawable getting tinted (#9410)
* fix my location drawable tinted when setting mapbox_myLocationTintColor in xml issue (linking my location foreground drawables with my location foreground tint color and adding the possibility of setting an undefined my location foreground tint color) * link my location background drawable with my location background tint color and add the possibility of setting an undefined my location background tint color
This commit is contained in:
@@ -4,7 +4,6 @@ import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Parcel;
|
||||
@@ -37,6 +36,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
|
||||
private static final float FOUR_DP = 4f;
|
||||
private static final float NINETY_TWO_DP = 92f;
|
||||
private static final int UNDEFINED_COLOR = -1;
|
||||
|
||||
private CameraPosition cameraPosition;
|
||||
|
||||
@@ -53,7 +53,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
private int[] logoMargins;
|
||||
|
||||
@ColorInt
|
||||
private int attributionTintColor = -1;
|
||||
private int attributionTintColor = UNDEFINED_COLOR;
|
||||
private boolean attributionEnabled = true;
|
||||
private int attributionGravity = Gravity.BOTTOM;
|
||||
private int[] attributionMargins;
|
||||
@@ -72,8 +72,10 @@ public class MapboxMapOptions implements Parcelable {
|
||||
private Drawable myLocationForegroundDrawable;
|
||||
private Drawable myLocationForegroundBearingDrawable;
|
||||
private Drawable myLocationBackgroundDrawable;
|
||||
private int myLocationForegroundTintColor;
|
||||
private int myLocationBackgroundTintColor;
|
||||
@ColorInt
|
||||
private int myLocationForegroundTintColor = UNDEFINED_COLOR;
|
||||
@ColorInt
|
||||
private int myLocationBackgroundTintColor = UNDEFINED_COLOR;
|
||||
private int[] myLocationBackgroundPadding;
|
||||
private int myLocationAccuracyTintColor;
|
||||
private int myLocationAccuracyAlpha;
|
||||
@@ -234,7 +236,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
FOUR_DP * pxlRatio))});
|
||||
|
||||
mapboxMapOptions.attributionTintColor(typedArray.getColor(
|
||||
R.styleable.mapbox_MapView_mapbox_uiAttributionTintColor, -1));
|
||||
R.styleable.mapbox_MapView_mapbox_uiAttributionTintColor, UNDEFINED_COLOR));
|
||||
mapboxMapOptions.attributionEnabled(typedArray.getBoolean(
|
||||
R.styleable.mapbox_MapView_mapbox_uiAttribution, true));
|
||||
mapboxMapOptions.attributionGravity(typedArray.getInt(
|
||||
@@ -251,10 +253,9 @@ public class MapboxMapOptions implements Parcelable {
|
||||
|
||||
mapboxMapOptions.locationEnabled(typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_myLocation, false));
|
||||
mapboxMapOptions.myLocationForegroundTintColor(
|
||||
typedArray.getColor(R.styleable.mapbox_MapView_mapbox_myLocationTintColor,
|
||||
ColorUtils.getPrimaryColor(context)));
|
||||
typedArray.getColor(R.styleable.mapbox_MapView_mapbox_myLocationTintColor, UNDEFINED_COLOR));
|
||||
mapboxMapOptions.myLocationBackgroundTintColor(
|
||||
typedArray.getColor(R.styleable.mapbox_MapView_mapbox_myLocationBackgroundTintColor, Color.WHITE));
|
||||
typedArray.getColor(R.styleable.mapbox_MapView_mapbox_myLocationBackgroundTintColor, UNDEFINED_COLOR));
|
||||
|
||||
Drawable foregroundDrawable = typedArray.getDrawable(R.styleable.mapbox_MapView_mapbox_myLocationDrawable);
|
||||
if (foregroundDrawable == null) {
|
||||
@@ -638,7 +639,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
/**
|
||||
* Set the background tint color of MyLocationView.
|
||||
*
|
||||
* @param myLocationBackgroundTintColor the color to tint the background
|
||||
* @param myLocationBackgroundTintColor the color to tint the background drawable
|
||||
* @return This
|
||||
*/
|
||||
public MapboxMapOptions myLocationBackgroundTintColor(@ColorInt int myLocationBackgroundTintColor) {
|
||||
@@ -944,6 +945,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
*
|
||||
* @return the tint color
|
||||
*/
|
||||
@ColorInt
|
||||
public int getMyLocationForegroundTintColor() {
|
||||
return myLocationForegroundTintColor;
|
||||
}
|
||||
@@ -953,6 +955,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
*
|
||||
* @return the tint color
|
||||
*/
|
||||
@ColorInt
|
||||
public int getMyLocationBackgroundTintColor() {
|
||||
return myLocationBackgroundTintColor;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ import timber.log.Timber;
|
||||
*/
|
||||
public class MyLocationView extends View {
|
||||
|
||||
private static final int UNDEFINED_TINT_COLOR = -1;
|
||||
private MyLocationBehavior myLocationBehavior;
|
||||
private MapboxMap mapboxMap;
|
||||
|
||||
@@ -198,12 +199,8 @@ public class MyLocationView extends View {
|
||||
* @param color The color to tint the drawable with
|
||||
*/
|
||||
public final void setForegroundDrawableTint(@ColorInt int color) {
|
||||
if (foregroundDrawable != null) {
|
||||
foregroundDrawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
if (foregroundBearingDrawable != null) {
|
||||
foregroundBearingDrawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
applyDrawableTint(foregroundDrawable, color);
|
||||
applyDrawableTint(foregroundBearingDrawable, color);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@@ -247,7 +244,7 @@ public class MyLocationView extends View {
|
||||
if (backgroundDrawable == null) {
|
||||
return;
|
||||
}
|
||||
backgroundDrawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
applyDrawableTint(backgroundDrawable, color);
|
||||
invalidate();
|
||||
}
|
||||
|
||||
@@ -737,6 +734,26 @@ public class MyLocationView extends View {
|
||||
setEnabled(isEnabled(), locationSource != null);
|
||||
}
|
||||
|
||||
private void applyDrawableTint(Drawable drawable, @ColorInt int color) {
|
||||
if (color == UNDEFINED_TINT_COLOR) {
|
||||
removeTintColorFilter(drawable);
|
||||
} else {
|
||||
applyTintColorFilter(drawable, color);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeTintColorFilter(Drawable drawable) {
|
||||
if (drawable != null) {
|
||||
drawable.mutate().setColorFilter(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void applyTintColorFilter(Drawable drawable, @ColorInt int color) {
|
||||
if (drawable != null) {
|
||||
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
}
|
||||
|
||||
private static class GpsLocationListener implements LocationEngineListener {
|
||||
|
||||
private WeakReference<MyLocationView> userLocationView;
|
||||
|
||||
@@ -119,6 +119,7 @@ public class MyLocationViewSettings {
|
||||
* <p>
|
||||
* The foreground drawable is the image visible on screen
|
||||
* </p>
|
||||
* It's linked with the foreground tint color
|
||||
*
|
||||
* @param foregroundDrawable the drawable to show as foreground without bearing
|
||||
* @param foregroundBearingDrawable the drawable to show as foreground when bearing is enabled
|
||||
@@ -127,6 +128,7 @@ public class MyLocationViewSettings {
|
||||
this.foregroundDrawable = foregroundDrawable;
|
||||
this.foregroundBearingDrawable = foregroundBearingDrawable;
|
||||
myLocationView.setForegroundDrawables(foregroundDrawable, foregroundBearingDrawable);
|
||||
myLocationView.setForegroundDrawableTint(foregroundTintColor);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,7 +155,8 @@ public class MyLocationViewSettings {
|
||||
* The color will tint both the foreground and the bearing foreground drawable.
|
||||
* </p>
|
||||
*
|
||||
* @param foregroundTintColor the color to tint the foreground drawable
|
||||
* @param foregroundTintColor the color to tint the foreground drawable or -1 (undefined color) to remove the
|
||||
* existing foreground tint color
|
||||
*/
|
||||
public void setForegroundTintColor(@ColorInt int foregroundTintColor) {
|
||||
this.foregroundTintColor = foregroundTintColor;
|
||||
@@ -174,6 +177,7 @@ public class MyLocationViewSettings {
|
||||
* <p>
|
||||
* Padding can be added to provide an offset to the background
|
||||
* </p>
|
||||
* It's linked with the background tint color
|
||||
*
|
||||
* @param backgroundDrawable the drawable to show as background
|
||||
* @param padding the padding added to the background
|
||||
@@ -186,6 +190,7 @@ public class MyLocationViewSettings {
|
||||
} else {
|
||||
myLocationView.setShadowDrawable(backgroundDrawable);
|
||||
}
|
||||
myLocationView.setShadowDrawableTint(backgroundTintColor);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,7 +205,8 @@ public class MyLocationViewSettings {
|
||||
/**
|
||||
* Set the background tint color.
|
||||
*
|
||||
* @param backgroundTintColor the color to tint the background
|
||||
* @param backgroundTintColor the color to tint the background drawable or -1 (undefined color) to remove the
|
||||
* existing background tint color
|
||||
*/
|
||||
public void setBackgroundTintColor(@ColorInt int backgroundTintColor) {
|
||||
this.backgroundTintColor = backgroundTintColor;
|
||||
|
||||
Reference in New Issue
Block a user