[android] ensure location shadow's gradient is greater than 0

This commit is contained in:
Łukasz Paczos
2019-07-11 12:21:59 +02:00
committed by Łukasz Paczos
parent 5b02434e88
commit afa7cea4c0
3 changed files with 70 additions and 5 deletions

View File

@@ -3,11 +3,15 @@ package com.mapbox.mapboxsdk.location;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.location.Location;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.log.Logger;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Projection;
@@ -15,6 +19,8 @@ import static com.mapbox.mapboxsdk.location.LocationComponentConstants.INSTANT_L
public final class Utils {
private static final String TAG = "Mbgl-com.mapbox.mapboxsdk.location.Utils";
private Utils() {
// Class should not be initialized
}
@@ -52,12 +58,42 @@ public final class Utils {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
try {
drawable.draw(canvas);
} catch (IllegalArgumentException ex) {
if (ex.getMessage().equals("radius must be > 0") && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Logger.w(TAG,
"Location's shadow gradient drawable has a radius <= 0px, resetting to 1px in order to avoid crashing");
ensureShadowGradientRadius(drawable);
return generateShadow(drawable, elevation);
} else {
throw ex;
}
}
bitmap = Bitmap.createScaledBitmap(bitmap,
toEven(width + elevation), toEven(height + elevation), false);
return bitmap;
}
/**
* We need to ensure that the radius of any {@link GradientDrawable} is greater than 0 for API levels < 21.
*
* @see <a href=https://github.com/mapbox/mapbox-gl-native/issues/15026>mapbox-gl-native-#15026</a>
*/
private static void ensureShadowGradientRadius(Drawable drawable) {
if (drawable instanceof GradientDrawable) {
((GradientDrawable) drawable).setGradientRadius(1);
} else if (drawable instanceof LayerDrawable) {
LayerDrawable layerDrawable = (LayerDrawable) drawable;
for (int i = 0; i < layerDrawable.getNumberOfLayers(); i++) {
Drawable layers = layerDrawable.getDrawable(i);
if (layers instanceof GradientDrawable) {
((GradientDrawable) layers).setGradientRadius(1);
}
}
}
}
static float calculateZoomLevelRadius(@NonNull MapboxMap mapboxMap, @Nullable Location location) {
if (location == null) {
return 0;