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

View file

@ -15,7 +15,12 @@ import org.apache.commons.logging.Log;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
public class RenderingIcons {
private static final Log log = PlatformUtil.getLog(RenderingIcons.class);
@ -25,6 +30,8 @@ public class RenderingIcons {
private static Map<String, Integer> bigIcons = new LinkedHashMap<String, Integer>();
private static Map<String, Bitmap> iconsBmp = new LinkedHashMap<String, Bitmap>();
// private static DisplayMetrics dm;
private static Bitmap cacheBmp = null;
public static boolean containsSmallIcon(String s){
return smallIcons.containsKey(s);
@ -33,6 +40,26 @@ public class RenderingIcons {
public static boolean containsBigIcon(String 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) {
Integer resId = shaderIcons.get(s);
@ -52,7 +79,10 @@ public class RenderingIcons {
}
inputStream.close();
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) ;
// if(dm != null){
// System.out.println("IC " + s +" " + dm.getHeight() + "x" + dm.getWidth());
@ -65,7 +95,22 @@ public class RenderingIcons {
return null;
}
}
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) {
Integer i = bigIcons.get(s);
if (i == null) {