Add support for vector icons

This commit is contained in:
Victor Shcherb 2020-04-29 17:40:15 +02:00
parent 65522a7ac0
commit b879a177eb
2 changed files with 53 additions and 5 deletions

View file

@ -84,7 +84,7 @@ android {
noCompress "qz" noCompress "qz"
cruncherEnabled = false cruncherEnabled = false
// Flag notifies aapt to keep the attribute IDs around // Flag notifies aapt to keep the attribute IDs around
// additionalParameters "--no-version-vectors" additionalParameters "--no-version-vectors"
} }
dexOptions { dexOptions {
@ -372,8 +372,11 @@ task copyMapShaderIcons(type: Sync) {
} }
task copyMapPOIIcons(type: Sync) { task copyMapPOIIcons(type: Sync) {
from "../../resources/rendering_styles/style-icons/map-icons-png" from "../../resources/rendering_styles/style-icons/map-icons-vector"
into "res/" into "res/drawable/"
// from "../../resources/rendering_styles/style-icons/map-icons-png"
// into "res/"
include "**/*.png", "**/*.xml" include "**/*.png", "**/*.xml"
preserve { preserve {
include '**/*' include '**/*'

View file

@ -15,7 +15,12 @@ import org.apache.commons.logging.Log;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
public class RenderingIcons { public class RenderingIcons {
private static final Log log = PlatformUtil.getLog(RenderingIcons.class); private static final Log log = PlatformUtil.getLog(RenderingIcons.class);
@ -26,6 +31,8 @@ public class RenderingIcons {
private static Map<String, Bitmap> iconsBmp = new LinkedHashMap<String, Bitmap>(); private static Map<String, Bitmap> iconsBmp = new LinkedHashMap<String, Bitmap>();
// private static DisplayMetrics dm; // private static DisplayMetrics dm;
private static Bitmap cacheBmp = null;
public static boolean containsSmallIcon(String s){ public static boolean containsSmallIcon(String s){
return smallIcons.containsKey(s); return smallIcons.containsKey(s);
} }
@ -34,6 +41,26 @@ public class RenderingIcons {
return bigIcons.containsKey(s); return bigIcons.containsKey(s);
} }
public static synchronized byte[] getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
if(cacheBmp == null || cacheBmp.getWidth() != drawable.getIntrinsicWidth() ||
cacheBmp.getHeight() != drawable.getIntrinsicHeight()) {
cacheBmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(cacheBmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
cacheBmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
public static byte[] getIconRawData(Context ctx, String s) { public static byte[] getIconRawData(Context ctx, String s) {
Integer resId = shaderIcons.get(s); Integer resId = shaderIcons.get(s);
if(resId == null) { if(resId == null) {
@ -52,7 +79,10 @@ public class RenderingIcons {
} }
inputStream.close(); inputStream.close();
final byte[] bitmapData = proxyOutputStream.toByteArray(); final byte[] bitmapData = proxyOutputStream.toByteArray();
log.info("Icon data length is " + bitmapData.length); //$NON-NLS-1$ if (isVectorData(bitmapData)) {
return getBitmapFromVectorDrawable(ctx, resId.intValue());
}
// log.info("Icon data length is " + bitmapData.length); //$NON-NLS-1$
// Bitmap dm = android.graphics.BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length) ; // Bitmap dm = android.graphics.BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length) ;
// if(dm != null){ // if(dm != null){
// System.out.println("IC " + s +" " + dm.getHeight() + "x" + dm.getWidth()); // System.out.println("IC " + s +" " + dm.getHeight() + "x" + dm.getWidth());
@ -66,6 +96,21 @@ public class RenderingIcons {
} }
} }
private static boolean isVectorData(byte[] bitmapData) {
for (int i = 0; i < bitmapData.length - 8 && i < 32; i++) {
int ind = 0;
if (bitmapData[i] == 'P' && bitmapData[i + 1] == 'N' &&
bitmapData[i + 2] == 'G') {
return false;
}
}
if (bitmapData.length > 4 && bitmapData[0] == 3 && bitmapData[1] == 0 &&
bitmapData[2] == 8 && bitmapData[3] == 0) {
return true;
}
return false;
}
public static int getBigIconResourceId(String s) { public static int getBigIconResourceId(String s) {
Integer i = bigIcons.get(s); Integer i = bigIcons.get(s);
if (i == null) { if (i == null) {