[android] #3231 - add support for tinting attribution icon through xml - MapboxMapOptions, AttributionDialog will use the same color values

This commit is contained in:
Tobrun
2016-04-26 09:52:14 +02:00
parent 3602fdc040
commit b39d289920
9 changed files with 106 additions and 44 deletions

View File

@@ -20,7 +20,6 @@ import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
@@ -83,7 +82,6 @@ import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.location.LocationListener;
import com.mapbox.mapboxsdk.location.LocationServices;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.maps.widgets.UserLocationView;
import com.mapbox.mapboxsdk.telemetry.MapboxEvent;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
@@ -266,24 +264,18 @@ public class MapView extends FrameLayout {
mMapboxMap.setStyleUrl(style);
}
mMapboxMap.setMyLocationEnabled(options.getLocationEnabled());
// Enable gestures
UiSettings uiSettings = mMapboxMap.getUiSettings();
uiSettings.setZoomGesturesEnabled(options.getZoomGesturesEnabled());
uiSettings.setZoomGestureChangeAllowed(options.getZoomGesturesEnabled());
uiSettings.setScrollGesturesEnabled(options.getScrollGesturesEnabled());
uiSettings.setScrollGestureChangeAllowed(options.getScrollGesturesEnabled());
uiSettings.setRotateGesturesEnabled(options.getRotateGesturesEnabled());
uiSettings.setRotateGestureChangeAllowed(options.getRotateGesturesEnabled());
uiSettings.setTiltGesturesEnabled(options.getTiltGesturesEnabled());
uiSettings.setTiltGestureChangeAllowed(options.getTiltGesturesEnabled());
uiSettings.setZoomControlsEnabled(options.getZoomControlsEnabled());
// Zoom
@@ -313,7 +305,6 @@ public class MapView extends FrameLayout {
}
// Attribution
ColorUtils.setTintList(mAttributionsView);
uiSettings.setAttributionEnabled(options.getAttributionEnabled());
uiSettings.setAttributionGravity(options.getAttributionGravity());
int[] attributionMargins = options.getAttributionMargins();
@@ -325,6 +316,10 @@ public class MapView extends FrameLayout {
int seventySixDp = (int) resources.getDimension(R.dimen.seventy_six_dp);
uiSettings.setAttributionMargins(seventySixDp, sevenDp, sevenDp, sevenDp);
}
int attributionTintColor = options.getAttributionTintColor();
uiSettings.setAttributionTintColor(attributionTintColor != -1 ?
attributionTintColor : ColorUtils.getPrimaryColor(getContext()));
}
//
@@ -906,7 +901,7 @@ public class MapView extends FrameLayout {
if (mDestroyed || location == null) {
return new PointF();
}
PointF pointF = mNativeMapView.pixelForLatLng(location);
PointF pointF = mNativeMapView.pixelForLatLng(location);
pointF.set(pointF.x * mScreenDensity, pointF.y * mScreenDensity);
return pointF;
}
@@ -2459,6 +2454,13 @@ public class MapView extends FrameLayout {
mAttributionsView.setVisibility(visibility);
}
void setAtttibutionTintColor(int tintColor) {
ColorUtils.setTintList(mAttributionsView, tintColor);
}
int getAttributionTintColor(){
return mMapboxMap.getUiSettings().getAttributionTintColor();
}
//
// Custom layer
@@ -2565,7 +2567,7 @@ public class MapView extends FrameLayout {
builder.setTitle(R.string.attributionsDialogTitle);
builder.setAdapter(new ArrayAdapter<>(context, R.layout.attribution_list_item, items), this);
AlertDialog dialog = builder.show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(ColorUtils.getPrimaryColor(context)));
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(mMapView.getAttributionTintColor()));
}
// Called when someone selects an attribution, 'Improve this map' adds location data to the url

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
@@ -40,6 +41,8 @@ public class MapboxMapOptions implements Parcelable {
private int logoGravity = Gravity.BOTTOM | Gravity.START;
private int logoMargins[];
@ColorInt
private int attributionTintColor = -1;
private boolean attributionEnabled = true;
private int attributionGravity = Gravity.BOTTOM;
private int attributionMargins[];
@@ -79,6 +82,7 @@ public class MapboxMapOptions implements Parcelable {
attributionEnabled = in.readByte() != 0;
attributionGravity = in.readInt();
attributionMargins = in.createIntArray();
attributionTintColor = in.readInt();
minZoom = in.readFloat();
maxZoom = in.readFloat();
@@ -137,6 +141,7 @@ public class MapboxMapOptions implements Parcelable {
, (int) (typedArray.getDimension(R.styleable.MapView_logo_margin_right, DIMENSION_SIXTEEN_DP) * screenDensity)
, (int) (typedArray.getDimension(R.styleable.MapView_logo_margin_bottom, DIMENSION_SIXTEEN_DP) * screenDensity)});
mapboxMapOptions.attributionTintColor(typedArray.getColor(R.styleable.MapView_attribution_tint, -1));
mapboxMapOptions.attributionEnabled(typedArray.getBoolean(R.styleable.MapView_attribution_enabled, true));
mapboxMapOptions.attributionGravity(typedArray.getInt(R.styleable.MapView_attribution_gravity, Gravity.BOTTOM));
mapboxMapOptions.attributionMargins(new int[]{(int) (typedArray.getDimension(R.styleable.MapView_attribution_margin_left, DIMENSION_SEVENTY_SIX_DP) * screenDensity)
@@ -316,6 +321,17 @@ public class MapboxMapOptions implements Parcelable {
return this;
}
/**
* Specifies the tint color of the attribution for a map view
*
* @param color integer resembling a color
* @return This
*/
public MapboxMapOptions attributionTintColor(@ColorInt int color) {
attributionTintColor = color;
return this;
}
/**
* Specifies if the rotate gesture is enabled for a map view.
*
@@ -553,6 +569,11 @@ public class MapboxMapOptions implements Parcelable {
return attributionMargins;
}
@ColorInt
public int getAttributionTintColor() {
return attributionTintColor;
}
/**
* Get the current configured user location view state for a map view.
*
@@ -603,6 +624,7 @@ public class MapboxMapOptions implements Parcelable {
dest.writeByte((byte) (attributionEnabled ? 1 : 0));
dest.writeInt(attributionGravity);
dest.writeIntArray(attributionMargins);
dest.writeInt(attributionTintColor);
dest.writeFloat(minZoom);
dest.writeFloat(maxZoom);

View File

@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.UiThread;
import android.view.Gravity;
@@ -239,11 +240,11 @@ public class UiSettings {
/**
* <p>
* Enables or disables the Mapbox logo.
* Enables or disables the attribution.
* </p>
* By default, the compass is enabled.
* By default, the attribution is enabled.
*
* @param enabled True to enable the logo; false to disable the logo.
* @param enabled True to enable the attribution; false to disable the attribution.
*/
public void setAttributionEnabled(boolean enabled) {
attributionSettings.setEnabled(enabled);
@@ -251,9 +252,9 @@ public class UiSettings {
}
/**
* Returns whether the logo is enabled.
* Returns whether the attribution is enabled.
*
* @return True if the logo is enabled; false if the logo is disabled.
* @return True if the attribution is enabled; false if the attribution is disabled.
*/
public boolean isAttributionEnabled() {
return attributionSettings.isEnabled();
@@ -261,10 +262,9 @@ public class UiSettings {
/**
* <p>
* Sets the gravity of the logo view. Use this to change the corner of the map view that the
* Mapbox logo is displayed in.
* Sets the gravity of the attribution.
* </p>
* By default, the logo is in the bottom left corner.
* By default, the attribution is in the bottom left corner next to the Mapbox logo.
*
* @param gravity One of the values from {@link Gravity}.
* @see Gravity
@@ -284,8 +284,7 @@ public class UiSettings {
}
/**
* Sets the margins of the logo view. Use this to change the distance of the Mapbox logo from the
* map view edge.
* Sets the margins of the attribution view.
*
* @param left The left margin in pixels.
* @param top The top margin in pixels.
@@ -298,7 +297,29 @@ public class UiSettings {
}
/**
* Returns the left side margin of the logo
* <p>
* Sets the tint of the attribution view. Use this to change the color of the attribution.
* </p>
* By default, the logo is tinted with the primary color of your theme.
*
* @param tintColor Color to tint the attribution.
*/
public void setAttributionTintColor(@ColorInt int tintColor) {
attributionSettings.setTintColor(tintColor);
mapView.setAtttibutionTintColor(tintColor);
}
/**
* Returns the tint color value of the attribution view.
*
* @return The tint color
*/
public int getAttributionTintColor() {
return attributionSettings.getTintColor();
}
/**
* Returns the left side margin of the attribution view.
*
* @return The left margin in pixels
*/
@@ -307,7 +328,7 @@ public class UiSettings {
}
/**
* Returns the top side margin of the logo
* Returns the top side margin of the attribution view.
*
* @return The top margin in pixels
*/
@@ -316,7 +337,7 @@ public class UiSettings {
}
/**
* Returns the right side margin of the logo
* Returns the right side margin of the attribution view.
*
* @return The right margin in pixels
*/
@@ -548,10 +569,20 @@ public class UiSettings {
mapView.setAttributionMargins(getAttributionMarginLeft(), getAttributionMarginTop(), getAttributionMarginRight(), getAttributionMarginBottom());
}
/**
* Returns the view settings related to the My Location View
*
* @return viewSettings used for My Location view.
*/
public MyLocationViewSettings getMyLocationViewSettings() {
return myLocationViewSettings;
}
/**
* Replaces the currenlty used view settings related to the My Location View.
*
* @param myLocationViewSettings The settings to be used.
*/
public void setMyLocationViewSettings(MyLocationViewSettings myLocationViewSettings) {
this.myLocationViewSettings = myLocationViewSettings;
}

View File

@@ -8,6 +8,7 @@ class ViewSettings {
private boolean enabled;
private int gravity;
private int[]margins;
private int tintColor;
public ViewSettings() {
margins = new int[4];
@@ -36,4 +37,12 @@ class ViewSettings {
public void setMargins(int[] margins) {
this.margins = margins;
}
public int getTintColor() {
return tintColor;
}
public void setTintColor(int tintColor) {
this.tintColor = tintColor;
}
}

View File

@@ -59,43 +59,32 @@ public class ColorUtils {
/**
* Returns a color state list associated with a theme based on a {@link Context}
*
* @param context The context used to style the color attributes.
* @param color The color used for tinting.
* @return A ColorStateList object containing the primary color of a theme
*/
@NonNull
public static ColorStateList getSelector(@NonNull Context context) {
public static ColorStateList getSelector(@ColorInt int color) {
return new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed},
new int[]{}
},
new int[]{
getPrimaryColor(context),
getPrimaryColor(context)
color,
color
}
);
}
/**
* Set a primary color tint list to the {@link Drawable} of an {@link ImageView}.
* Set a color tint list to the {@link Drawable} of an {@link ImageView}.
*
* @param imageView The view to set the default tint list.
* @param tintColor The color to tint.
*/
public static void setTintList(@NonNull ImageView imageView) {
public static void setTintList(@NonNull ImageView imageView, @ColorInt int tintColor) {
Drawable originalDrawable = imageView.getDrawable();
Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
DrawableCompat.setTintList(wrappedDrawable, getSelector(imageView.getContext()));
DrawableCompat.setTintList(wrappedDrawable, getSelector(tintColor));
}
/**
* Set a primary color tint to a {@link Drawable}.
*
* @param context The context used to style the color attributes.
* @param drawable The drawable to tint.
*/
public static void setTint(@NonNull Context context, @NonNull Drawable drawable) {
Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrappedDrawable, ColorUtils.getPrimaryColor(context));
}
}