[android] - add support for sdf by exposing sdf confiigurationn when calling MapboxMap#addImage.

This commit is contained in:
tobrun
2018-06-20 14:18:15 +02:00
committed by Tobrun
parent e1af62e87d
commit 279dd93f7c
7 changed files with 64 additions and 22 deletions

View File

@@ -6,12 +6,14 @@ class Image {
private final String name;
private final int width;
private final int height;
private final boolean sdf;
public Image(byte[] buffer, float pixelRatio, String name, int width, int height) {
public Image(byte[] buffer, float pixelRatio, String name, int width, int height, boolean sdf) {
this.buffer = buffer;
this.pixelRatio = pixelRatio;
this.name = name;
this.width = width;
this.height = height;
this.sdf = sdf;
}
}

View File

@@ -450,7 +450,18 @@ public final class MapboxMap {
* @param image the pre-multiplied Bitmap
*/
public void addImage(@NonNull String name, @NonNull Bitmap image) {
nativeMapView.addImage(name, image);
addImage(name, image, false);
}
/**
* Adds an image to be used in the map's style
*
* @param name the name of the image
* @param image the pre-multiplied Bitmap
* @param sdf the flag indicating image is an SDF or template image
*/
public void addImage(@NonNull String name, @NonNull Bitmap image, boolean sdf) {
nativeMapView.addImage(name, image, sdf);
}
/**

View File

@@ -787,22 +787,29 @@ final class NativeMapView {
return source;
}
public void addImage(@NonNull String name, @NonNull Bitmap image) {
public void addImage(@NonNull String name, @NonNull Bitmap image, boolean sdf) {
if (checkState("addImage")) {
return;
}
// Determine pixel ratio, cast to float to avoid rounding, see mapbox-gl-native/issues/11809
float pixelRatio = (float) image.getDensity() / DisplayMetrics.DENSITY_DEFAULT;
nativeAddImage(name, image, pixelRatio);
nativeAddImage(name, image, pixelRatio, sdf);
}
public void addImages(@NonNull HashMap<String, Bitmap> bitmapHashMap) {
if (checkState("addImages")) {
return;
}
this.addImages(bitmapHashMap, false);
}
public void addImages(@NonNull HashMap<String, Bitmap> bitmapHashMap, boolean sdf) {
if (checkState("addImages")) {
return;
}
//noinspection unchecked
new BitmapImageConversionTask(this).execute(bitmapHashMap);
new BitmapImageConversionTask(this, sdf).execute(bitmapHashMap);
}
public void removeImage(String name) {
@@ -1061,7 +1068,7 @@ final class NativeMapView {
private native void nativeRemoveSource(Source source, long sourcePtr);
private native void nativeAddImage(String name, Bitmap bitmap, float pixelRatio);
private native void nativeAddImage(String name, Bitmap bitmap, float pixelRatio, boolean sdf);
private native void nativeAddImages(Image[] images);
@@ -1161,9 +1168,11 @@ final class NativeMapView {
private static class BitmapImageConversionTask extends AsyncTask<HashMap<String, Bitmap>, Void, List<Image>> {
private NativeMapView nativeMapView;
private boolean sdf;
BitmapImageConversionTask(NativeMapView nativeMapView) {
BitmapImageConversionTask(NativeMapView nativeMapView, boolean sdf) {
this.nativeMapView = nativeMapView;
this.sdf = sdf;
}
@Override
@@ -1186,10 +1195,9 @@ final class NativeMapView {
buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
float density = bitmap.getDensity() == Bitmap.DENSITY_NONE ? Bitmap.DENSITY_NONE : bitmap.getDensity();
float pixelRatio = density / DisplayMetrics.DENSITY_DEFAULT;
float pixelRatio = (float) bitmap.getDensity() / DisplayMetrics.DENSITY_DEFAULT;
images.add(new Image(buffer.array(), pixelRatio, name, bitmap.getWidth(), bitmap.getHeight()));
images.add(new Image(buffer.array(), pixelRatio, name, bitmap.getWidth(), bitmap.getHeight(), sdf));
}
return images;