summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java')
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java56
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java22
2 files changed, 78 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
index 14fc69f456..567f0fc768 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/Style.java
@@ -1,17 +1,20 @@
package com.mapbox.mapboxsdk.maps;
import android.graphics.Bitmap;
+import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import android.util.DisplayMetrics;
+
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.layers.TransitionOptions;
import com.mapbox.mapboxsdk.style.light.Light;
import com.mapbox.mapboxsdk.style.sources.Source;
+import com.mapbox.mapboxsdk.utils.BitmapUtils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -299,6 +302,20 @@ public class Style {
}
/**
+ * Adds an drawable to be converted into a bitmap to be used in the map's style
+ *
+ * @param name the name of the image
+ * @param drawable the drawable instance to convert
+ */
+ public void addImage(@NonNull String name, @NonNull Drawable drawable) {
+ Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(drawable);
+ if (bitmap == null) {
+ throw new IllegalArgumentException("Provided drawable couldn't be converted to a Bitmap.");
+ }
+ addImage(name, bitmap, false);
+ }
+
+ /**
* Adds an image to be used in the map's style
*
* @param name the name of the image
@@ -312,6 +329,8 @@ public class Style {
/**
* Adds an images to be used in the map's style.
+ *
+ * @param images the map of images to add
*/
public void addImages(@NonNull HashMap<String, Bitmap> images) {
addImages(images, false);
@@ -319,6 +338,9 @@ public class Style {
/**
* Adds an images to be used in the map's style.
+ *
+ * @param images the map of images to add
+ * @param sdf the flag indicating image is an SDF or template image
*/
public void addImages(@NonNull HashMap<String, Bitmap> images, boolean sdf) {
validateState("addImages");
@@ -631,6 +653,22 @@ public class Style {
}
/**
+ * Will add the drawable as image when the map style has loaded.
+ *
+ * @param id the id for the image
+ * @param drawable the drawable to be converted and added
+ * @return this
+ */
+ @NonNull
+ public Builder withImage(@NonNull String id, @NonNull Drawable drawable) {
+ Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(drawable);
+ if (bitmap == null) {
+ throw new IllegalArgumentException("Provided drawable couldn't be converted to a Bitmap.");
+ }
+ return this.withImage(id, bitmap, false);
+ }
+
+ /**
* Will add the image when the map style has loaded.
*
* @param id the id for the image
@@ -643,10 +681,28 @@ public class Style {
}
/**
+ * Will add the drawable as image when the map style has loaded.
+ *
+ * @param id the id for the image
+ * @param drawable the drawable to be converted and added
+ * @param sdf the flag indicating image is an SDF or template image
+ * @return this
+ */
+ @NonNull
+ public Builder withImage(@NonNull String id, @NonNull Drawable drawable, boolean sdf) {
+ Bitmap bitmap = BitmapUtils.getBitmapFromDrawable(drawable);
+ if (bitmap == null) {
+ throw new IllegalArgumentException("Provided drawable couldn't be converted to a Bitmap.");
+ }
+ return this.withImage(id, bitmap, sdf);
+ }
+
+ /**
* Will add the image when the map style has loaded.
*
* @param id the id for the image
* @param image the image to be added
+ * @param sdf the flag indicating image is an SDF or template image
* @return this
*/
@NonNull
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java
index 5584a6eb58..f14e49cb6a 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java
@@ -8,9 +8,12 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
+import android.support.annotation.VisibleForTesting;
import android.view.View;
import java.io.ByteArrayOutputStream;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
/**
* Utility class for creating bitmaps
@@ -121,4 +124,23 @@ public class BitmapUtils {
return new BitmapDrawable(context.getResources(), compass);
}
+
+ /**
+ * Validates if the bytes of a bitmap matches another
+ *
+ * @param bitmap the bitmap to be compared against
+ * @param other the bitmap to compare with
+ * @return true if equal
+ */
+ @VisibleForTesting
+ public static boolean equals(Bitmap bitmap, Bitmap other) {
+ ByteBuffer buffer = ByteBuffer.allocate(bitmap.getHeight() * bitmap.getRowBytes());
+ bitmap.copyPixelsToBuffer(buffer);
+
+ ByteBuffer bufferOther = ByteBuffer.allocate(other.getHeight() * other.getRowBytes());
+ other.copyPixelsToBuffer(bufferOther);
+
+ return Arrays.equals(buffer.array(), bufferOther.array());
+ }
+
}