[android] - add bulk addImages api on MapboxMap

This commit is contained in:
Tobrun
2017-10-24 17:43:53 -07:00
committed by Tobrun
parent ebe6a30821
commit f578b26282
10 changed files with 278 additions and 49 deletions

View File

@@ -0,0 +1,17 @@
package com.mapbox.mapboxsdk.maps;
class Image {
private final byte[] buffer;
private final float pixelRatio;
private final String name;
private final int width;
private final int height;
public Image(byte[] buffer, float pixelRatio, String name, int width, int height) {
this.buffer = buffer;
this.pixelRatio = pixelRatio;
this.name = name;
this.width = width;
this.height = height;
}
}

View File

@@ -47,6 +47,7 @@ import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.Geometry;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.List;
import timber.log.Timber;
@@ -466,6 +467,13 @@ public final class MapboxMap {
nativeMapView.addImage(name, image);
}
/**
* Adds an images to be used in the map's style
*/
public void addImages(@NonNull HashMap<String, Bitmap> images) {
nativeMapView.addImages(images);
}
/**
* Removes an image from the map's style
*
@@ -1674,9 +1682,9 @@ public final class MapboxMap {
/**
* Get a camera position that fits a provided shape with a given bearing and padding.
*
* @param geometry the geometry to constrain the map with
* @param bearing the bearing at which to compute the geometry's bounds
* @param padding the padding to apply to the bounds
* @param geometry the geometry to constrain the map with
* @param bearing the bearing at which to compute the geometry's bounds
* @param padding the padding to apply to the bounds
* @return the camera position that fits the bounds and padding
*/
public CameraPosition getCameraForGeometry(Geometry geometry, double bearing, int[] padding) {

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@@ -34,7 +35,9 @@ import com.mapbox.services.commons.geojson.Geometry;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import timber.log.Timber;
@@ -753,6 +756,7 @@ final class NativeMapView {
if (isDestroyedOn("addImage")) {
return;
}
// Check/correct config
if (image.getConfig() != Bitmap.Config.ARGB_8888) {
image = image.copy(Bitmap.Config.ARGB_8888, false);
@@ -769,6 +773,14 @@ final class NativeMapView {
nativeAddImage(name, image.getWidth(), image.getHeight(), pixelRatio, buffer.array());
}
public void addImages(@NonNull HashMap<String, Bitmap> bitmapHashMap) {
if (isDestroyedOn("addImages")) {
return;
}
//noinspection unchecked
new BitmapImageConversionTask(this).execute(bitmapHashMap);
}
public void removeImage(String name) {
if (isDestroyedOn("removeImage")) {
return;
@@ -1024,6 +1036,8 @@ final class NativeMapView {
private native void nativeAddImage(String name, int width, int height, float pixelRatio,
byte[] array);
private native void nativeAddImages(Image[] images);
private native void nativeRemoveImage(String name);
private native Bitmap nativeGetImage(String name);
@@ -1111,4 +1125,55 @@ final class NativeMapView {
});
}
//
// Image conversion
//
private static class BitmapImageConversionTask extends AsyncTask<HashMap<String, Bitmap>, Void, List<Image>> {
private NativeMapView nativeMapView;
BitmapImageConversionTask(NativeMapView nativeMapView) {
this.nativeMapView = nativeMapView;
}
@Override
protected List<Image> doInBackground(HashMap<String, Bitmap>... params) {
HashMap<String, Bitmap> bitmapHashMap = params[0];
List<Image> images = new ArrayList<>();
ByteBuffer buffer;
String name;
Bitmap bitmap;
for (Map.Entry<String, Bitmap> stringBitmapEntry : bitmapHashMap.entrySet()) {
name = stringBitmapEntry.getKey();
bitmap = stringBitmapEntry.getValue();
if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);
}
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;
images.add(new Image(buffer.array(), pixelRatio, name, bitmap.getWidth(), bitmap.getHeight()));
}
return images;
}
@Override
protected void onPostExecute(List<Image> images) {
super.onPostExecute(images);
if (nativeMapView != null && !nativeMapView.isDestroyedOn("nativeAddImages")) {
nativeMapView.nativeAddImages(images.toArray(new Image[images.size()]));
}
}
}
}