diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java | 149 |
1 files changed, 92 insertions, 57 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java index 1bf5d25b4a..442850f0e4 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/Style.java @@ -1,111 +1,146 @@ package com.mapbox.mapboxsdk.style; import android.graphics.Bitmap; +import android.os.AsyncTask; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.DisplayMetrics; import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.maps.Image; +import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException; 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.CannotAddSourceException; import com.mapbox.mapboxsdk.style.sources.Source; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class Style { - void loadJson(String json) { + private Style(){ } - void loadURL(String url) { - + public static Style fromJson(String json){ + return new Style(); } - String getJson() { - return ""; + public static Style fromUrl(String url) { + return new Style(); } - String getURL() { - return ""; + public void addImage(String id, Bitmap image){ + if (image.getConfig() != Bitmap.Config.ARGB_8888) { + image = image.copy(Bitmap.Config.ARGB_8888, false); + } + float pixelRatio = image.getDensity() / DisplayMetrics.DENSITY_DEFAULT; + ByteBuffer buffer = ByteBuffer.allocate(image.getByteCount()); + image.copyPixelsToBuffer(buffer); + nativeAddImage(id, image.getWidth(), image.getHeight(), pixelRatio, buffer.array()); } - String getName() { - return ""; + public void addImages(@NonNull HashMap<String, Bitmap> bitmapHashMap) { + //noinspection unchecked + new BitmapImageConversionTask(this).execute(bitmapHashMap); } - CameraPosition getDefaultCamera() { - return null; - } + public native String getJson(); - TransitionOptions getTransitionOptions() { - return null; - } + public native String getURL(); - void setTransitionOptions(TransitionOptions options) { - } + public native String getName(); - void setLight(Light light) { - } + public native CameraPosition getDefaultCamera(); - Light getLight() { - return null; - } + public native TransitionOptions getTransitionOptions(); - Bitmap getImage(String image) { - return null; - } + public native void setTransitionOptions(TransitionOptions options); - void addImage(String id, Bitmap bitmap) { + public native void setLight(Light light); - } + public native void getLight(); - void removeImage(String id) { + public native Bitmap getImage(String image); - } + private native void nativeAddImage(String name, int width, int height, float pixelRatio, byte[] array); - //@NonNull - List<Source> getSources() { - return null; - } + private native void nativeAddImages(Image[] images); - @Nullable - Source getSource(String id) { - return null; - } + public native void removeImage(String id); - void addSource(Source source) { + private native Layer nativeGetLayer(String layerId); - } + private native void nativeAddLayer(long layerPtr, String before) throws CannotAddLayerException; - void removeSource(Source source) { + private native void nativeAddLayerAbove(long layerPtr, String above) throws CannotAddLayerException; - } + private native void nativeAddLayerAt(long layerPtr, int index) throws CannotAddLayerException; - List<Layer> getLayers() { - return null; - } + private native Layer nativeRemoveLayerById(String layerId); - @Nullable - Layer getLayer(String layerId) { - return null; - } + private native void nativeRemoveLayer(long layerId); - public void addLayer(@NonNull Layer layer) { - } + private native Layer nativeRemoveLayerAt(int index); - public void addLayerBelow(@NonNull Layer layer, @NonNull String below) { - } + private native Source[] nativeGetSources(); - public void addLayerAbove(@NonNull Layer layer, @NonNull String above) { - } + private native Source nativeGetSource(String sourceId); - public void addLayerAt(@NonNull Layer layer, @IntRange(from = 0) int index) { - } + private native void nativeAddSource(Source source, long sourcePtr) throws CannotAddSourceException; + + private native void nativeRemoveSource(Source source, long sourcePtr); + + private static class BitmapImageConversionTask extends AsyncTask<HashMap<String, Bitmap>, Void, List<Image>> { + + private Style style; + + BitmapImageConversionTask(Style style) { + this.style = style; + } + + @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())); + } - void removeLayer(String id) { + return images; + } + @Override + protected void onPostExecute(List<Image> images) { + super.onPostExecute(images); + if (style != null) { + style.nativeAddImages(images.toArray(new Image[images.size()])); + } + } } } |