[android] add changing compass view image feature to the sdk (#8301)

This commit is contained in:
Pablo Guardiola
2017-03-14 16:31:17 +01:00
committed by GitHub
parent 96a53156c2
commit 30c848b9d8
6 changed files with 113 additions and 4 deletions

View File

@@ -121,6 +121,7 @@ public class MapboxConstants {
public static final String STATE_COMPASS_MARGIN_RIGHT = "mapbox_compassMarginRight";
public static final String STATE_COMPASS_MARGIN_BOTTOM = "mapbox_compassMarginBottom";
public static final String STATE_COMPASS_FADE_WHEN_FACING_NORTH = "mapbox_compassFade";
public static final String STATE_COMPASS_IMAGE_BITMAP = "mapbox_compassImage";
public static final String STATE_LOGO_GRAVITY = "mapbox_logoGravity";
public static final String STATE_LOGO_MARGIN_LEFT = "mapbox_logoMarginLeft";
public static final String STATE_LOGO_MARGIN_TOP = "mapbox_logoMarginTop";

View File

@@ -47,6 +47,7 @@ public class MapboxMapOptions implements Parcelable {
private boolean fadeCompassFacingNorth = true;
private int compassGravity = Gravity.TOP | Gravity.END;
private int[] compassMargins;
private Drawable compassImage;
private boolean logoEnabled = true;
private int logoGravity = Gravity.BOTTOM | Gravity.START;
@@ -100,6 +101,11 @@ public class MapboxMapOptions implements Parcelable {
compassMargins = in.createIntArray();
fadeCompassFacingNorth = in.readByte() != 0;
Bitmap compassBitmap = in.readParcelable(getClass().getClassLoader());
if (compassBitmap != null) {
compassImage = new BitmapDrawable(compassBitmap);
}
logoEnabled = in.readByte() != 0;
logoGravity = in.readInt();
logoMargins = in.createIntArray();
@@ -147,7 +153,7 @@ public class MapboxMapOptions implements Parcelable {
textureMode = in.readByte() != 0;
}
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
static Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
@@ -208,6 +214,12 @@ public class MapboxMapOptions implements Parcelable {
DIMENSION_TEN_DP * pxlRatio))});
mapboxMapOptions.compassFadesWhenFacingNorth(typedArray.getBoolean(
R.styleable.mapbox_MapView_mapbox_uiCompassFadeFacingNorth, true));
Drawable compassDrawable = typedArray.getDrawable(
R.styleable.mapbox_MapView_mapbox_uiCompassDrawable);
if (compassDrawable == null) {
compassDrawable = ContextCompat.getDrawable(context, R.drawable.mapbox_compass_icon);
}
mapboxMapOptions.compassImage(compassDrawable);
mapboxMapOptions.logoEnabled(typedArray.getBoolean(R.styleable.mapbox_MapView_mapbox_uiLogo, true));
mapboxMapOptions.logoGravity(typedArray.getInt(R.styleable.mapbox_MapView_mapbox_uiLogoGravity,
@@ -400,6 +412,20 @@ public class MapboxMapOptions implements Parcelable {
return this;
}
/**
* Specifies the image of the CompassView.
* <p>
* By default this value is R.drawable.mapbox_compass_icon.
* </p>
*
* @param compass the drawable to show as image compass
* @return This
*/
public MapboxMapOptions compassImage(Drawable compass) {
this.compassImage = compass;
return this;
}
/**
* Specifies the visibility state of a logo for a map view.
*
@@ -742,6 +768,15 @@ public class MapboxMapOptions implements Parcelable {
return fadeCompassFacingNorth;
}
/**
* Get the current configured CompassView image.
*
* @return the drawable used as compass image
*/
public Drawable getCompassImage() {
return compassImage;
}
/**
* Get the current configured visibility state for mapbox_compass_icon for a map view.
*
@@ -993,6 +1028,8 @@ public class MapboxMapOptions implements Parcelable {
dest.writeInt(compassGravity);
dest.writeIntArray(compassMargins);
dest.writeByte((byte) (fadeCompassFacingNorth ? 1 : 0));
dest.writeParcelable(compassImage != null
? getBitmapFromDrawable(compassImage) : null, flags);
dest.writeByte((byte) (logoEnabled ? 1 : 0));
dest.writeInt(logoGravity);
@@ -1052,6 +1089,11 @@ public class MapboxMapOptions implements Parcelable {
if (fadeCompassFacingNorth != options.fadeCompassFacingNorth) {
return false;
}
if (compassImage != null
? !compassImage.equals(options.compassImage)
: options.compassImage != null) {
return false;
}
if (compassGravity != options.compassGravity) {
return false;
}
@@ -1157,6 +1199,7 @@ public class MapboxMapOptions implements Parcelable {
result = 31 * result + (compassEnabled ? 1 : 0);
result = 31 * result + (fadeCompassFacingNorth ? 1 : 0);
result = 31 * result + compassGravity;
result = 31 * result + (compassImage != null ? compassImage.hashCode() : 0);
result = 31 * result + Arrays.hashCode(compassMargins);
result = 31 * result + (logoEnabled ? 1 : 0);
result = 31 * result + logoGravity;

View File

@@ -3,8 +3,12 @@ package com.mapbox.mapboxsdk.maps;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
@@ -22,6 +26,8 @@ import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import com.mapbox.mapboxsdk.utils.ColorUtils;
import java.io.ByteArrayOutputStream;
/**
* Settings for the user interface of a MapboxMap. To obtain this interface, call getUiSettings().
*/
@@ -143,6 +149,7 @@ public final class UiSettings {
setCompassMargins(tenDp, tenDp, tenDp, tenDp);
}
setCompassFadeFacingNorth(options.getCompassFadeFacingNorth());
setCompassImage(options.getCompassImage());
}
private void saveCompass(Bundle outState) {
@@ -153,6 +160,16 @@ public final class UiSettings {
outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM, getCompassMarginBottom());
outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT, getCompassMarginRight());
outState.putBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH, isCompassFadeWhenFacingNorth());
outState.putByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP,
convert(MapboxMapOptions.getBitmapFromDrawable(getCompassImage())));
}
private byte[] convert(Bitmap resource) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resource.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
private void restoreCompass(Bundle savedInstanceState) {
@@ -163,6 +180,15 @@ public final class UiSettings {
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT),
savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM));
setCompassFadeFacingNorth(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH));
setCompassImage(decode(savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
}
private Drawable decode(byte[] bitmap) {
Bitmap compass = BitmapFactory.decodeByteArray(bitmap, 0, bitmap.length);
Drawable compassImage = new BitmapDrawable(compassView.getResources(), compass);
return compassImage;
}
private void initialiseLogo(MapboxMapOptions options, Resources resources) {
@@ -296,6 +322,18 @@ public final class UiSettings {
compassView.fadeCompassViewFacingNorth(compassFadeFacingNorth);
}
/**
* Specifies the CompassView image.
* <p>
* By default this value is R.drawable.mapbox_compass_icon.
* </p>
*
* @param compass the drawable to show as image compass
*/
public void setCompassImage(Drawable compass) {
compassView.setCompassImage(compass);
}
/**
* Returns whether the compass performs a fading animation out when facing north.
*
@@ -364,6 +402,15 @@ public final class UiSettings {
return ((FrameLayout.LayoutParams) compassView.getLayoutParams()).bottomMargin;
}
/**
* Get the current configured CompassView image.
*
* @return the drawable used as compass image
*/
public Drawable getCompassImage() {
return compassView.getCompassImage();
}
void update(@NonNull CameraPosition cameraPosition) {
if (!isCompassEnabled()) {
return;

View File

@@ -2,9 +2,9 @@ package com.mapbox.mapboxsdk.maps.widgets;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
@@ -13,7 +13,6 @@ import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
import com.mapbox.mapboxsdk.maps.MapboxMap;
@@ -55,7 +54,6 @@ public final class CompassView extends AppCompatImageView implements Runnable, F
}
private void initialize(Context context) {
setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.mapbox_compass_icon));
setEnabled(false);
// Layout params
@@ -140,6 +138,24 @@ public final class CompassView extends AppCompatImageView implements Runnable, F
return fadeCompassViewFacingNorth;
}
/**
* Set the CompassView image.
*
* @param compass the drawable to use as compass image
*/
public void setCompassImage(Drawable compass) {
setImageDrawable(compass);
}
/**
* Get the current configured CompassView image.
*
* @return the drawable used as compass image
*/
public Drawable getCompassImage() {
return getDrawable();
}
@Override
public void run() {
if (isFacingNorth() && fadeCompassViewFacingNorth) {