From acd6641ea22d1bff9486dfa23dcd8e6a4ec1c487 Mon Sep 17 00:00:00 2001 From: Matt Reid Date: Tue, 2 Jul 2019 23:38:49 +0100 Subject: [PATCH] Android icon image scaling works. --- .../mapbox/mapboxgl/MapboxMapController.java | 90 +++++++++++++++---- 1 file changed, 72 insertions(+), 18 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 3dda73627..0cf07915d 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -54,11 +54,7 @@ import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import static com.mapbox.mapboxgl.MapboxMapsPlugin.CREATED; @@ -412,14 +408,16 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) { Convert.interpretSymbolOptions(call.argument("options"), symbolBuilder); final Symbol symbol = symbolBuilder.build(); final String symbolId = String.valueOf(symbol.getId()); - try { // Will throw if image does not exist (assumes it's a default icon) + + // Will throw if image does not exist (assumes it's a default icon) + try { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); - float density = displayMetrics.density; - Bitmap bitmap = getImage(symbol, density); + Bitmap bitmap = getImage(symbol, displayMetrics.density); mapboxMap.getStyle().addImage(symbol.getIconImage(), bitmap); } catch (IOException e) { e.printStackTrace(); } + symbols.put(symbolId, new SymbolController(symbol, true, this)); result.success(symbolId); break; @@ -490,16 +488,6 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) { } } - public Bitmap getImage(Symbol symbol, float density) throws IOException { - AssetManager assetManager = registrar.context().getAssets(); - String assetPath = registrar.lookupKeyForAsset(symbol.getIconImage()); - AssetFileDescriptor assetFileDescriptor = assetManager.openFd(assetPath); - InputStream a = assetFileDescriptor.createInputStream(); - Bitmap bitmap = BitmapFactory.decodeStream(a); - assetFileDescriptor.close(); - return bitmap; - } - @Override public void onCameraMoveStarted(int reason) { final Map arguments = new HashMap<>(2); @@ -763,4 +751,70 @@ private int checkSelfPermission(String permission) { permission, android.os.Process.myPid(), android.os.Process.myUid()); } + // Tries to find highest scale image for display type + private Bitmap getImage(Symbol symbol, float density) throws IOException { + AssetManager assetManager = registrar.context().getAssets(); + String assetPath; + AssetFileDescriptor assetFileDescriptor = null; + + // Split possible path into + List imagePathList = new ArrayList<>(Arrays.asList(symbol.getIconImage().split("/"))); + + if (imagePathList.size() > 1) { // Should stop most default icons + for (int i = (int) density; i >= 1; i--) { // Goes down by 1 each time (e.g. 3.0 -> 2.0 -> 1.0) + + // If on last size, set default value + if (i == 1) { + assetPath = registrar.lookupKeyForAsset(symbol.getIconImage()); + assetFileDescriptor = assetManager.openFd(assetPath); + break; + } + + // Add possible size to list + String strDensity = Float.toString((float) i) + "x"; + int size = imagePathList.size(); + imagePathList.add(size - 1, strDensity); + + // Turn array into a string + String imagePath; + StringBuilder stringBuilder = new StringBuilder(); + for (int j = 0; j < imagePathList.size(); j++) { + stringBuilder.append(imagePathList.get(j)); + if (j != imagePathList.size() - 1) { + stringBuilder.append("/"); + } + } + imagePath = stringBuilder.toString(); + + // Reset for next time + imagePathList = Arrays.asList(symbol.getIconImage().split("/")); + + // Get possible path + assetPath = registrar.lookupKeyForAsset(imagePath); + + + try { + // Read path (throws if doesn't exist) + assetFileDescriptor = assetManager.openFd(assetPath); + break; // If exists, break + } catch (IOException e) { + if (assetFileDescriptor != null) { + assetFileDescriptor.close(); // Path doesn't exist but still close for memory + } + } + } + } + + if (assetFileDescriptor == null) { // Verify it's not a default icon + assetPath = registrar.lookupKeyForAsset(symbol.getIconImage()); + assetFileDescriptor = assetManager.openFd(assetPath); // Sets back to default + } + + InputStream assetStream = assetFileDescriptor.createInputStream(); + Bitmap bitmap = BitmapFactory.decodeStream(assetStream); + assetFileDescriptor.close(); // Close for memory + + return bitmap; + } + }