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

Commit

Permalink
link my location background drawable with my location background tint…
Browse files Browse the repository at this point in the history
… color and add the possibility of setting an undefined my location background tint color
  • Loading branch information
Guardiola31337 committed Jul 6, 2017
1 parent 86084b5 commit 4c36782
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
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;
Expand Down Expand Up @@ -75,7 +74,8 @@ public class MapboxMapOptions implements Parcelable {
private Drawable myLocationBackgroundDrawable;
@ColorInt
private int myLocationForegroundTintColor = UNDEFINED_COLOR;
private int myLocationBackgroundTintColor;
@ColorInt
private int myLocationBackgroundTintColor = UNDEFINED_COLOR;
private int[] myLocationBackgroundPadding;
private int myLocationAccuracyTintColor;
private int myLocationAccuracyAlpha;
Expand Down Expand Up @@ -255,7 +255,7 @@ public static MapboxMapOptions createFromAttributes(@NonNull Context context, @N
mapboxMapOptions.myLocationForegroundTintColor(
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) {
Expand Down Expand Up @@ -639,7 +639,7 @@ public MapboxMapOptions myLocationForegroundTintColor(@ColorInt int myLocationFo
/**
* 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) {
Expand Down Expand Up @@ -955,6 +955,7 @@ public int getMyLocationForegroundTintColor() {
*
* @return the tint color
*/
@ColorInt
public int getMyLocationBackgroundTintColor() {
return myLocationBackgroundTintColor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
*/
public class MyLocationView extends View {

private static final int UNDEFINED_FOREGROUND_TINT_COLOR = -1;
private static final int UNDEFINED_TINT_COLOR = -1;
private MyLocationBehavior myLocationBehavior;
private MapboxMap mapboxMap;

Expand Down Expand Up @@ -199,11 +199,8 @@ public final void setForegroundDrawables(Drawable defaultDrawable, Drawable bear
* @param color The color to tint the drawable with
*/
public final void setForegroundDrawableTint(@ColorInt int color) {
if (color == UNDEFINED_FOREGROUND_TINT_COLOR) {
removeForegroundTintColorFilter();
} else {
applyForegroundTintColorFilter(color);
}
applyDrawableTint(foregroundDrawable, color);
applyDrawableTint(foregroundBearingDrawable, color);
invalidate();
}

Expand Down Expand Up @@ -247,7 +244,7 @@ public final void setShadowDrawableTint(@ColorInt int color) {
if (backgroundDrawable == null) {
return;
}
backgroundDrawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
applyDrawableTint(backgroundDrawable, color);
invalidate();
}

Expand Down Expand Up @@ -737,21 +734,23 @@ public void setLocationSource(LocationEngine locationSource) {
setEnabled(isEnabled(), locationSource != null);
}

private void removeForegroundTintColorFilter() {
if (foregroundDrawable != null) {
foregroundDrawable.mutate().setColorFilter(null);
}
if (foregroundBearingDrawable != null) {
foregroundBearingDrawable.mutate().setColorFilter(null);
private void applyDrawableTint(Drawable drawable, @ColorInt int color) {
if (color == UNDEFINED_TINT_COLOR) {
removeTintColorFilter(drawable);
} else {
applyTintColorFilter(drawable, color);
}
}

private void applyForegroundTintColorFilter(@ColorInt int color) {
if (foregroundDrawable != null) {
foregroundDrawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
private void removeTintColorFilter(Drawable drawable) {
if (drawable != null) {
drawable.mutate().setColorFilter(null);
}
if (foregroundBearingDrawable != null) {
foregroundBearingDrawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}

private void applyTintColorFilter(Drawable drawable, @ColorInt int color) {
if (drawable != null) {
drawable.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
public class MyLocationViewSettings {

private static final int UNDEFINED_FOREGROUND_TINT_COLOR = -1;
private Projection projection;
private MyLocationView myLocationView;
private FocalPointChangeListener focalPointChangeListener;
Expand Down Expand Up @@ -178,6 +177,7 @@ public int getForegroundTintColor() {
* <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
Expand All @@ -190,6 +190,7 @@ public void setBackgroundDrawable(Drawable backgroundDrawable, int[] padding) {
} else {
myLocationView.setShadowDrawable(backgroundDrawable);
}
myLocationView.setShadowDrawableTint(backgroundTintColor);
}

/**
Expand All @@ -204,7 +205,8 @@ public Drawable getBackgroundDrawable() {
/**
* 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;
Expand Down

0 comments on commit 4c36782

Please sign in to comment.