[android] - add support for sdf by exposing sdf confiigurationn when calling MapboxMap#addImage.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.style;
|
||||
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PointF;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
@@ -18,17 +19,28 @@ import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.style.layers.Property;
|
||||
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
|
||||
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.utils.BitmapUtils;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.any;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.get;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.has;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.literal;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.lte;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.not;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAllowOverlap;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconAnchor;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconColor;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconImage;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.iconSize;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textAnchor;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textFont;
|
||||
@@ -54,11 +66,13 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
mapView.getMapAsync(map -> {
|
||||
mapboxMap = map;
|
||||
|
||||
// Add a image for the makers
|
||||
// Add a sdf image for the makers
|
||||
Drawable icLayersDrawable = getResources().getDrawable(R.drawable.ic_layers);
|
||||
Bitmap icLayersBitmap = BitmapUtils.getBitmapFromDrawable(icLayersDrawable);
|
||||
mapboxMap.addImage(
|
||||
"my-marker-image",
|
||||
BitmapFactory.decodeResource(SymbolLayerActivity.this.getResources(),
|
||||
R.drawable.mapbox_marker_icon_default)
|
||||
"my-layers-image",
|
||||
icLayersBitmap,
|
||||
true
|
||||
);
|
||||
|
||||
// Add a source
|
||||
@@ -72,12 +86,15 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
mapboxMap.addLayer(
|
||||
new SymbolLayer(MARKER_LAYER, MARKER_SOURCE)
|
||||
.withProperties(
|
||||
iconImage("my-marker-image"),
|
||||
iconImage("my-layers-image"),
|
||||
iconAllowOverlap(true),
|
||||
textField("{title}"),
|
||||
iconAnchor(Property.ICON_ANCHOR_BOTTOM),
|
||||
textField(get("title")),
|
||||
iconColor(Color.RED),
|
||||
textColor(Color.RED),
|
||||
textAnchor(Property.TEXT_ANCHOR_TOP),
|
||||
textSize(10f)
|
||||
)
|
||||
).withFilter((any(not(has("price")), lte(get("price"), literal(25)))))
|
||||
);
|
||||
|
||||
// Show
|
||||
|
||||
@@ -11,6 +11,7 @@ mbgl::style::Image Image::getImage(jni::JNIEnv& env, jni::Object<Image> image) {
|
||||
static auto pixelRatioField = Image::javaClass.GetField<jni::jfloat>(env, "pixelRatio");
|
||||
static auto bufferField = Image::javaClass.GetField<jni::Array<jbyte>>(env, "buffer");
|
||||
static auto nameField = Image::javaClass.GetField<jni::String>(env, "name");
|
||||
static auto sdfField = Image::javaClass.GetField<jni::jboolean>(env, "sdf");
|
||||
|
||||
auto height = image.Get(env, heightField);
|
||||
auto width = image.Get(env, widthField);
|
||||
@@ -18,6 +19,7 @@ mbgl::style::Image Image::getImage(jni::JNIEnv& env, jni::Object<Image> image) {
|
||||
auto pixels = image.Get(env, bufferField);
|
||||
auto jName = image.Get(env, nameField);
|
||||
auto name = jni::Make<std::string>(env, jName);
|
||||
auto sdf = (bool) image.Get(env, sdfField);
|
||||
jni::DeleteLocalRef(env, jName);
|
||||
|
||||
jni::NullCheck(env, &pixels);
|
||||
@@ -30,7 +32,7 @@ mbgl::style::Image Image::getImage(jni::JNIEnv& env, jni::Object<Image> image) {
|
||||
|
||||
jni::GetArrayRegion(env, *pixels, 0, size, reinterpret_cast<jbyte*>(premultipliedImage.data.get()));
|
||||
jni::DeleteLocalRef(env, pixels);
|
||||
return mbgl::style::Image {name, std::move(premultipliedImage), pixelRatio};
|
||||
return mbgl::style::Image {name, std::move(premultipliedImage), pixelRatio, sdf};
|
||||
}
|
||||
|
||||
void Image::registerNative(jni::JNIEnv &env) {
|
||||
|
||||
@@ -895,14 +895,16 @@ void NativeMapView::removeSource(JNIEnv& env, jni::Object<Source> obj, jlong sou
|
||||
source->removeFromMap(env, obj, *map);
|
||||
}
|
||||
|
||||
void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::Object<Bitmap> bitmap, jni::jfloat scale) {
|
||||
void NativeMapView::addImage(JNIEnv& env, jni::String name, jni::Object<Bitmap> bitmap, jni::jfloat scale, jni::jboolean sdf) {
|
||||
jni::NullCheck(env, &bitmap);
|
||||
mbgl::PremultipliedImage premultipliedImage = Bitmap::GetImage(env, bitmap);
|
||||
|
||||
map->getStyle().addImage(std::make_unique<mbgl::style::Image>(
|
||||
jni::Make<std::string>(env, name),
|
||||
std::move(premultipliedImage),
|
||||
float(scale)));
|
||||
float(scale),
|
||||
sdf)
|
||||
);
|
||||
}
|
||||
|
||||
void NativeMapView::addImages(JNIEnv& env, jni::Array<jni::Object<mbgl::android::Image>> jimages) {
|
||||
|
||||
@@ -236,7 +236,7 @@ public:
|
||||
|
||||
void removeSource(JNIEnv&, jni::Object<Source>, jlong nativePtr);
|
||||
|
||||
void addImage(JNIEnv&, jni::String, jni::Object<Bitmap> bitmap, jni::jfloat);
|
||||
void addImage(JNIEnv&, jni::String, jni::Object<Bitmap> bitmap, jni::jfloat, jni::jboolean);
|
||||
|
||||
void addImages(JNIEnv&, jni::Array<jni::Object<mbgl::android::Image>>);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user