Saving/restoring MyLocationViewSettings (#10746)
[android] saving/restoring MyLocationViewSettings, updated BitmapUtils
This commit is contained in:
@@ -135,4 +135,15 @@ public class MapboxConstants {
|
||||
public static final String STATE_ATTRIBUTION_ENABLED = "mapbox_atrrEnabled";
|
||||
public static final String STATE_LOCATION_CHANGE_ANIMATION_ENABLED = "mapbox_locationChangeAnimationEnabled";
|
||||
public static final String STATE_USING_CUSTOM_LOCATION_SOURCE = "mapbox_usingCustomLocationSource";
|
||||
public static final String STATE_LOCATION_VIEW_ENABLED = "mapbox_locViewEnabled";
|
||||
public static final String STATE_LOCATION_VIEW_FOREGROUND_DRAWABLE = "mapbox_locViewForegroundDrawable";
|
||||
public static final String STATE_LOCATION_VIEW_FOREGROUND_BEARING_DRAWABLE = "mapbox_locViewBearingDrawable";
|
||||
public static final String STATE_LOCATION_VIEW_FOREGROUND_TINT_COLOR = "mapbox_locViewForegroundTintColor";
|
||||
public static final String STATE_LOCATION_VIEW_BACKGROUND_DRAWABLE = "mapbox_locViewBackgroundDrawable";
|
||||
public static final String STATE_LOCATION_VIEW_BACKGROUND_OFFSET = "mapbox_locViewBackgroundOffset";
|
||||
public static final String STATE_LOCATION_VIEW_BACKGROUND_TINT_COLOR = "mapbox_locViewBackgroundTintColor";
|
||||
public static final String STATE_LOCATION_VIEW_ACCURACY_ALPHA = "mapbox_locViewAccuracyAlpha";
|
||||
public static final String STATE_LOCATION_VIEW_ACCURACY_TINT_COLOR = "mapbox_locViewAccuracyTintColor";
|
||||
public static final String STATE_LOCATION_VIEW_ACCURACY_THRESHOLD = "mapbox_locViewAccuracyThreshold";
|
||||
public static final String STATE_LOCATION_VIEW_PADDING = "mapbox_locViewPadding";
|
||||
}
|
||||
|
||||
@@ -136,6 +136,7 @@ public final class MapboxMap {
|
||||
outState.putString(MapboxConstants.STATE_STYLE_URL, nativeMapView.getStyleUrl());
|
||||
trackingSettings.onSaveInstanceState(outState);
|
||||
uiSettings.onSaveInstanceState(outState);
|
||||
myLocationViewSettings.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,6 +147,7 @@ public final class MapboxMap {
|
||||
void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
final CameraPosition cameraPosition = savedInstanceState.getParcelable(MapboxConstants.STATE_CAMERA_POSITION);
|
||||
|
||||
myLocationViewSettings.onRestoreInstanceState(savedInstanceState);
|
||||
uiSettings.onRestoreInstanceState(savedInstanceState);
|
||||
trackingSettings.onRestoreInstanceState(savedInstanceState);
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.mapbox.mapboxsdk.maps;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Parcel;
|
||||
@@ -20,6 +19,7 @@ import android.view.Gravity;
|
||||
import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.utils.BitmapUtils;
|
||||
import com.mapbox.mapboxsdk.utils.ColorUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -161,19 +161,6 @@ public class MapboxMapOptions implements Parcelable {
|
||||
localIdeographFontFamily = in.readString();
|
||||
}
|
||||
|
||||
static Bitmap getBitmapFromDrawable(Drawable drawable) {
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
return ((BitmapDrawable) drawable).getBitmap();
|
||||
} else {
|
||||
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a MapboxMapsOptions from the attribute set.s
|
||||
*
|
||||
@@ -1130,7 +1117,7 @@ public class MapboxMapOptions implements Parcelable {
|
||||
dest.writeIntArray(compassMargins);
|
||||
dest.writeByte((byte) (fadeCompassFacingNorth ? 1 : 0));
|
||||
dest.writeParcelable(compassImage != null
|
||||
? getBitmapFromDrawable(compassImage) : null, flags);
|
||||
? BitmapUtils.getBitmapFromDrawable(compassImage) : null, flags);
|
||||
|
||||
dest.writeByte((byte) (logoEnabled ? 1 : 0));
|
||||
dest.writeInt(logoGravity);
|
||||
@@ -1154,11 +1141,11 @@ public class MapboxMapOptions implements Parcelable {
|
||||
dest.writeByte((byte) (myLocationEnabled ? 1 : 0));
|
||||
|
||||
dest.writeParcelable(myLocationForegroundDrawable != null
|
||||
? getBitmapFromDrawable(myLocationForegroundDrawable) : null, flags);
|
||||
? BitmapUtils.getBitmapFromDrawable(myLocationForegroundDrawable) : null, flags);
|
||||
dest.writeParcelable(myLocationForegroundBearingDrawable != null
|
||||
? getBitmapFromDrawable(myLocationForegroundBearingDrawable) : null, flags);
|
||||
? BitmapUtils.getBitmapFromDrawable(myLocationForegroundBearingDrawable) : null, flags);
|
||||
dest.writeParcelable(myLocationBackgroundDrawable != null
|
||||
? getBitmapFromDrawable(myLocationBackgroundDrawable) : null, flags);
|
||||
? BitmapUtils.getBitmapFromDrawable(myLocationBackgroundDrawable) : null, flags);
|
||||
dest.writeInt(myLocationForegroundTintColor);
|
||||
dest.writeInt(myLocationBackgroundTintColor);
|
||||
dest.writeIntArray(myLocationBackgroundPadding);
|
||||
|
||||
@@ -3,11 +3,8 @@ 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;
|
||||
@@ -24,10 +21,9 @@ import com.mapbox.mapboxsdk.R;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
|
||||
import com.mapbox.mapboxsdk.utils.BitmapUtils;
|
||||
import com.mapbox.mapboxsdk.utils.ColorUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* Settings for the user interface of a MapboxMap. To obtain this interface, call getUiSettings().
|
||||
*/
|
||||
@@ -170,13 +166,7 @@ public final class UiSettings {
|
||||
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);
|
||||
return stream.toByteArray();
|
||||
BitmapUtils.getByteArrayFromDrawable(getCompassImage()));
|
||||
}
|
||||
|
||||
private void restoreCompass(Bundle savedInstanceState) {
|
||||
@@ -187,12 +177,8 @@ 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);
|
||||
return new BitmapDrawable(compassView.getResources(), compass);
|
||||
setCompassImage(BitmapUtils.getDrawableFromByteArray(
|
||||
compassView.getContext(), savedInstanceState.getByteArray(MapboxConstants.STATE_COMPASS_IMAGE_BITMAP)));
|
||||
}
|
||||
|
||||
private void initialiseLogo(MapboxMapOptions options, Resources resources) {
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
package com.mapbox.mapboxsdk.maps.widgets;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.ColorInt;
|
||||
import android.support.annotation.IntRange;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
|
||||
import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
|
||||
import com.mapbox.mapboxsdk.maps.Projection;
|
||||
import com.mapbox.mapboxsdk.utils.BitmapUtils;
|
||||
|
||||
/**
|
||||
* Settings to configure the visual appearance of the MyLocationView.
|
||||
*
|
||||
* @deprecated use location layer plugin from
|
||||
* https://github.com/mapbox/mapbox-plugins-android/tree/master/plugins/locationlayer instead.
|
||||
*/
|
||||
@@ -100,6 +104,56 @@ public class MyLocationViewSettings {
|
||||
setAccuracyThreshold(options.getMyLocationAccuracyThreshold());
|
||||
}
|
||||
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
outState.putBoolean(MapboxConstants.STATE_LOCATION_VIEW_ENABLED, isEnabled());
|
||||
outState.putByteArray(
|
||||
MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_DRAWABLE,
|
||||
BitmapUtils.getByteArrayFromDrawable(getForegroundDrawable())
|
||||
);
|
||||
outState.putByteArray(
|
||||
MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_BEARING_DRAWABLE,
|
||||
BitmapUtils.getByteArrayFromDrawable(getForegroundBearingDrawable())
|
||||
);
|
||||
outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_TINT_COLOR, getForegroundTintColor());
|
||||
outState.putByteArray(
|
||||
MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_DRAWABLE,
|
||||
BitmapUtils.getByteArrayFromDrawable(getBackgroundDrawable())
|
||||
);
|
||||
outState.putIntArray(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_OFFSET, getBackgroundOffset());
|
||||
outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_TINT_COLOR, getBackgroundTintColor());
|
||||
outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_ALPHA, getAccuracyAlpha());
|
||||
outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_TINT_COLOR, getAccuracyTintColor());
|
||||
outState.putFloat(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_THRESHOLD, getAccuracyThreshold());
|
||||
outState.putIntArray(MapboxConstants.STATE_LOCATION_VIEW_PADDING, getPadding());
|
||||
}
|
||||
|
||||
public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
|
||||
setEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_LOCATION_VIEW_ENABLED));
|
||||
setForegroundDrawable(
|
||||
BitmapUtils.getDrawableFromByteArray(
|
||||
myLocationView.getContext(),
|
||||
savedInstanceState.getByteArray(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_DRAWABLE)
|
||||
),
|
||||
BitmapUtils.getDrawableFromByteArray(
|
||||
myLocationView.getContext(),
|
||||
savedInstanceState.getByteArray(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_BEARING_DRAWABLE)
|
||||
)
|
||||
);
|
||||
setForegroundTintColor(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_TINT_COLOR));
|
||||
setBackgroundDrawable(
|
||||
BitmapUtils.getDrawableFromByteArray(
|
||||
myLocationView.getContext(),
|
||||
savedInstanceState.getByteArray(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_DRAWABLE)
|
||||
),
|
||||
savedInstanceState.getIntArray(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_OFFSET)
|
||||
);
|
||||
setBackgroundTintColor(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_TINT_COLOR));
|
||||
setAccuracyAlpha(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_ALPHA));
|
||||
setAccuracyTintColor(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_TINT_COLOR));
|
||||
setAccuracyThreshold(savedInstanceState.getFloat(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_THRESHOLD));
|
||||
setPadding(savedInstanceState.getIntArray(MapboxConstants.STATE_LOCATION_VIEW_PADDING));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if the MyLocationView is enabled
|
||||
*
|
||||
@@ -246,6 +300,10 @@ public class MyLocationViewSettings {
|
||||
*/
|
||||
public void setPadding(int left, int top, int right, int bottom) {
|
||||
padding = new int[] {left, top, right, bottom};
|
||||
setPadding(padding);
|
||||
}
|
||||
|
||||
private void setPadding(int[] padding) {
|
||||
myLocationView.setContentPadding(padding);
|
||||
projection.invalidateContentPadding(padding);
|
||||
invalidateFocalPointForTracking(myLocationView);
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
package com.mapbox.mapboxsdk.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.view.View;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
/**
|
||||
* Utility class for creating bitmaps
|
||||
*/
|
||||
@@ -46,4 +52,51 @@ public class BitmapUtils {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract an underlying bitmap from a drawable
|
||||
* @param drawable The source drawable
|
||||
* @return The underlying bitmap
|
||||
*/
|
||||
public static Bitmap getBitmapFromDrawable(Drawable drawable) {
|
||||
if (drawable instanceof BitmapDrawable) {
|
||||
return ((BitmapDrawable) drawable).getBitmap();
|
||||
} else {
|
||||
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
|
||||
Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a byte array out of drawable
|
||||
* @param drawable The source drawable
|
||||
* @return The byte array of source drawable
|
||||
*/
|
||||
public static byte[] getByteArrayFromDrawable(Drawable drawable) {
|
||||
if (drawable == null) {
|
||||
return null;
|
||||
}
|
||||
Bitmap bitmap = getBitmapFromDrawable(drawable);
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
|
||||
return stream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode byte array to drawable object
|
||||
* @param context Context to obtain {@link android.content.res.Resources}
|
||||
* @param array The source byte array
|
||||
* @return The drawable created from source byte array
|
||||
*/
|
||||
public static Drawable getDrawableFromByteArray(Context context, byte[] array) {
|
||||
if (array == null) {
|
||||
return null;
|
||||
}
|
||||
Bitmap compass = BitmapFactory.decodeByteArray(array, 0, array.length);
|
||||
return new BitmapDrawable(context.getResources(), compass);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user