diff options
author | Pablo Guardiola <guardiola31337@gmail.com> | 2017-03-14 16:31:17 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-14 16:31:17 +0100 |
commit | 30c848b9d8558d3bd669252c27b5ca6a6c7abb8c (patch) | |
tree | 591a60cf4747348a03edc13eb81c1f453f55d547 | |
parent | 96a53156c277a2fc03a83134da55d7c72a79a441 (diff) | |
download | qtlocation-mapboxgl-30c848b9d8558d3bd669252c27b5ca6a6c7abb8c.tar.gz |
[android] add changing compass view image feature to the sdk (#8301)
6 files changed, 113 insertions, 4 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java index 0bd9523f4f..9adefa3221 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/constants/MapboxConstants.java @@ -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"; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java index 6467033ead..2fd02c76e5 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMapOptions.java @@ -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, @@ -401,6 +413,20 @@ public class MapboxMapOptions implements Parcelable { } /** + * 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. * * @param enabled True and logo is shown @@ -743,6 +769,15 @@ public class MapboxMapOptions implements Parcelable { } /** + * 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. * * @return Visibility state of the mapbox_compass_icon @@ -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; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java index 8a3ae1e4f3..bcb4ca4afc 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/UiSettings.java @@ -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) { @@ -297,6 +323,18 @@ public final class UiSettings { } /** + * 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. * * @return True if the compass will fade, false if it remains visible @@ -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; diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java index 5c9cf93ebc..48bfae4a6e 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java @@ -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) { diff --git a/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml b/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml index 641020906a..7283aced5f 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml +++ b/platform/android/MapboxGLAndroidSDK/src/main/res-public/values/public.xml @@ -54,6 +54,7 @@ <public name="mapbox_uiCompassMarginRight" type="attr" /> <public name="mapbox_uiCompassMarginBottom" type="attr" /> <public name="mapbox_uiCompassFadeFacingNorth" type="attr" /> + <public name="mapbox_uiCompassDrawable" type="attr" /> <!--Logo--> <public name="mapbox_uiLogo" type="attr" /> diff --git a/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml b/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml index 738cae07be..e17f01d075 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml +++ b/platform/android/MapboxGLAndroidSDK/src/main/res/values/attrs.xml @@ -64,6 +64,7 @@ <attr name="mapbox_uiCompassMarginRight" format="dimension"/> <attr name="mapbox_uiCompassMarginBottom" format="dimension"/> <attr name="mapbox_uiCompassFadeFacingNorth" format="boolean"/> + <attr name="mapbox_uiCompassDrawable" format="reference"/> <!--Logo--> <attr name="mapbox_uiLogo" format="boolean"/> |