[android] get vector drawables using AppCompatResources and set vector drawables programmatically

This commit is contained in:
Łukasz Paczos
2019-03-07 12:20:46 +01:00
committed by Łukasz Paczos
parent b4e315ee28
commit a0fd38eb29
9 changed files with 63 additions and 58 deletions

View File

@@ -9,13 +9,13 @@ import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.exceptions.TooManyIconsException;
import com.mapbox.mapboxsdk.utils.BitmapUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -30,7 +30,7 @@ import java.io.InputStream;
*
* @deprecated As of 7.0.0,
* use <a href="https://github.com/mapbox/mapbox-plugins-android/tree/master/plugin-annotation">
* Mapbox Annotation Plugin</a> instead
* Mapbox Annotation Plugin</a> instead
*/
@Deprecated
public final class IconFactory {
@@ -100,7 +100,7 @@ public final class IconFactory {
* @return The icon that was loaded from the asset or {@code null} if failed to load.
*/
public Icon fromResource(@DrawableRes int resourceId) {
Drawable drawable = ContextCompat.getDrawable(context, resourceId);
Drawable drawable = BitmapUtils.getDrawableFromRes(context, resourceId);
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return fromBitmap(bitmapDrawable.getBitmap());

View File

@@ -6,13 +6,11 @@ import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.utils.BitmapUtils;
import static com.mapbox.mapboxsdk.location.Utils.generateShadow;
import static com.mapbox.mapboxsdk.location.Utils.getBitmapFromDrawable;
import static com.mapbox.mapboxsdk.location.Utils.getDrawable;
class LayerBitmapProvider {
@@ -23,12 +21,12 @@ class LayerBitmapProvider {
}
Bitmap generateBitmap(@DrawableRes int drawableRes, @ColorInt Integer tintColor) {
Drawable drawable = getDrawable(context, drawableRes, tintColor);
return getBitmapFromDrawable(drawable);
Drawable drawable = BitmapUtils.getDrawableFromRes(context, drawableRes, tintColor);
return BitmapUtils.getBitmapFromDrawable(drawable);
}
Bitmap generateShadowBitmap(@NonNull LocationComponentOptions options) {
Drawable shadowDrawable = ContextCompat.getDrawable(context, R.drawable.mapbox_user_icon_shadow);
Drawable shadowDrawable = BitmapUtils.getDrawableFromRes(context, R.drawable.mapbox_user_icon_shadow);
return generateShadow(shadowDrawable, options.elevation());
}
}

View File

@@ -1,18 +1,11 @@
package com.mapbox.mapboxsdk.location;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
@@ -43,20 +36,6 @@ public final class Utils {
return heading;
}
static Bitmap getBitmapFromDrawable(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else {
// width and height are equal for all assets since they are ovals.
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
static Bitmap generateShadow(Drawable drawable, float elevation) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
@@ -69,22 +48,6 @@ public final class Utils {
return bitmap;
}
@Nullable
static Drawable getDrawable(@NonNull Context context, @DrawableRes int drawableRes,
@Nullable @ColorInt Integer tintColor) {
Drawable drawable = ContextCompat.getDrawable(context, drawableRes);
if (tintColor == null) {
return drawable;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.setTint(tintColor);
} else {
drawable.mutate().setColorFilter(tintColor, PorterDuff.Mode.SRC_IN);
}
return drawable;
}
static float calculateZoomLevelRadius(@NonNull MapboxMap mapboxMap, @Nullable Location location) {
if (location == null) {
return 0;

View File

@@ -129,7 +129,9 @@ public class MapView extends FrameLayout implements NativeMapView.ViewCallback {
View view = LayoutInflater.from(context).inflate(R.layout.mapbox_mapview_internal, this);
compassView = view.findViewById(R.id.compassView);
attrView = view.findViewById(R.id.attributionView);
attrView.setImageDrawable(BitmapUtils.getDrawableFromRes(getContext(), R.drawable.mapbox_info_bg_selector));
logoView = view.findViewById(R.id.logoView);
logoView.setImageDrawable(BitmapUtils.getDrawableFromRes(getContext(), R.drawable.mapbox_logo_icon));
// add accessibility support
setContentDescription(context.getString(R.string.mapbox_mapActionDescription));

View File

@@ -9,10 +9,10 @@ import android.support.annotation.Keep;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.content.ContextCompat;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.geometry.LatLngQuad;
import com.mapbox.mapboxsdk.utils.BitmapUtils;
import java.net.URL;
@@ -115,7 +115,7 @@ public class ImageSource extends Source {
public void setImage(@DrawableRes int resourceId) throws IllegalArgumentException {
checkThread();
Context context = Mapbox.getApplicationContext();
Drawable drawable = ContextCompat.getDrawable(context, resourceId);
Drawable drawable = BitmapUtils.getDrawableFromRes(context, resourceId);
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
nativeSetImage(bitmapDrawable.getBitmap());

View File

@@ -4,11 +4,16 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v7.content.res.AppCompatResources;
import android.view.View;
import java.io.ByteArrayOutputStream;
@@ -124,6 +129,45 @@ public class BitmapUtils {
return new BitmapDrawable(context.getResources(), compass);
}
/**
* Get a drawable from a resource.
*
* @param context Context to obtain {@link android.content.res.Resources}
* @param drawableRes Drawable resource
* @return The drawable created from the resource
*/
@Nullable
public static Drawable getDrawableFromRes(@NonNull Context context, @DrawableRes int drawableRes) {
return getDrawableFromRes(context, drawableRes, null);
}
/**
* Get a tinted drawable from a resource.
*
* @param context Context to obtain {@link android.content.res.Resources}
* @param drawableRes Drawable resource
* @param tintColor Tint color
* @return The drawable created from the resource
*/
@Nullable
public static Drawable getDrawableFromRes(@NonNull Context context, @DrawableRes int drawableRes,
@Nullable @ColorInt Integer tintColor) {
Drawable drawable = AppCompatResources.getDrawable(context, drawableRes);
if (drawable == null) {
return null;
}
if (tintColor == null) {
return drawable;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.setTint(tintColor);
} else {
drawable.mutate().setColorFilter(tintColor, PorterDuff.Mode.SRC_IN);
}
return drawable;
}
/**
* Validates if the bytes of a bitmap matches another