implement VectorDrawable, by loading tmp SVG with gdkpixbuf

This won't work for Apps using VectorDrawableCompat
This commit is contained in:
Julian Winkler
2024-03-24 15:41:39 +01:00
parent 26b6da2c9c
commit f4a0191b1c
4 changed files with 62 additions and 1 deletions

View File

@@ -41,5 +41,5 @@ public class BitmapDrawable extends Drawable {
a.recycle();
}
private static native long native_paintable_from_pixbuf(long pixbuf);
static native long native_paintable_from_pixbuf(long pixbuf);
}

View File

@@ -134,6 +134,10 @@ public class Drawable {
} else if ("ripple".equals(parser.getName())) {
// FIXME: the non-pressed state of RippleDrawable should be equivalent to this
return new ColorDrawable(0);
} else if ("vector".equals(parser.getName())) {
VectorDrawable drawable = new VectorDrawable();
drawable.inflate(resources, parser, parser, null);
return drawable;
}
return null;
}

View File

@@ -0,0 +1,56 @@
package android.graphics.drawable;
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.android.internal.R;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.res.TypedArray;
import android.util.AttributeSet;
public class VectorDrawable extends Drawable {
public VectorDrawable() {
super();
}
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) throws XmlPullParserException, IOException {
final int innerDepth = parser.getDepth() + 1;
int type;
TypedArray a = r.obtainAttributes(attrs, R.styleable.VectorDrawable);
int tint = a.getColor(R.styleable.VectorDrawable_tint, 0);
float viewportWidth = a.getFloat(R.styleable.VectorDrawable_viewportWidth, 24);
float viewportHeight = a.getFloat(R.styleable.VectorDrawable_viewportHeight, 24);
a.recycle();
StringBuilder sb = new StringBuilder();
sb.append(String.format("<svg id=\"vector\" xmlns=\"http://www.w3.org/2000/svg\" width=\"%f\" height=\"%f\" viewBox=\"0 0 %f %f\">",
viewportWidth, viewportHeight, viewportWidth, viewportHeight));
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (parser.getDepth() >= innerDepth
|| type != XmlPullParser.END_TAG)) {
if (type == XmlPullParser.START_TAG && parser.getName().equals("path")) {
a = r.obtainAttributes(attrs, R.styleable.VectorDrawablePath);
String pathData = a.getString(R.styleable.VectorDrawablePath_pathData);
int fillColor = tint != 0 ? tint : a.getColor(R.styleable.VectorDrawablePath_fillColor, 0);
int strokeColor = tint != 0 ? tint : a.getColor(R.styleable.VectorDrawablePath_strokeColor, 0);
float strokeWidth = a.getFloat(R.styleable.VectorDrawablePath_strokeWidth, 0);
a.recycle();
sb.append(String.format("<path fill=\"#%06x%02x\" stroke=\"#%06x%02x\" stroke-width=\"%f\" d=\"%s\"/>",
fillColor & 0xFFFFFF, (fillColor >> 24) & 0xFF, strokeColor & 0xFFFFFF, (strokeColor >> 24) & 0xFF, strokeWidth, pathData));
}
}
sb.append("</svg>");
String svg = sb.toString();
byte[] bytes = svg.getBytes();
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
this.paintable = BitmapDrawable.native_paintable_from_pixbuf(bm.pixbuf);
}
}

View File

@@ -187,6 +187,7 @@ hax_jar = jar('hax', [
'android/graphics/drawable/ScaleDrawable.java',
'android/graphics/drawable/ShapeDrawable.java',
'android/graphics/drawable/StateListDrawable.java',
'android/graphics/drawable/VectorDrawable.java',
'android/graphics/drawable/shapes/OvalShape.java',
'android/graphics/drawable/shapes/Shape.java',
'android/hardware/ConsumerIrManager.java',