VectorDrawable: generate Bitmap in the correct size

This commit is contained in:
Julian Winkler
2024-12-15 00:52:09 +01:00
parent 9023e2963b
commit af122a6891

View File

@@ -24,6 +24,11 @@ public class VectorDrawable extends Drawable {
private Bitmap bitmap; private Bitmap bitmap;
private Paint paint = new Paint(); private Paint paint = new Paint();
private float viewportWidth;
private float viewportHeight;
private float width;
private float height;
private String svg;
public VectorDrawable() { public VectorDrawable() {
super(); super();
@@ -37,14 +42,12 @@ public class VectorDrawable extends Drawable {
setColorFilter(a.getColor(R.styleable.VectorDrawable_tint, 0), setColorFilter(a.getColor(R.styleable.VectorDrawable_tint, 0),
PorterDuff.Mode.values()[a.getInt(R.styleable.VectorDrawable_tintMode, DEFAULT_TINT_MODE.nativeInt)]); PorterDuff.Mode.values()[a.getInt(R.styleable.VectorDrawable_tintMode, DEFAULT_TINT_MODE.nativeInt)]);
} }
float viewportWidth = a.getFloat(R.styleable.VectorDrawable_viewportWidth, 24); viewportWidth = a.getFloat(R.styleable.VectorDrawable_viewportWidth, 24);
float viewportHeight = a.getFloat(R.styleable.VectorDrawable_viewportHeight, 24); viewportHeight = a.getFloat(R.styleable.VectorDrawable_viewportHeight, 24);
float width = a.getDimension(R.styleable.VectorDrawable_width, 24); width = a.getDimension(R.styleable.VectorDrawable_width, 24);
float height = a.getDimension(R.styleable.VectorDrawable_height, 24); height = a.getDimension(R.styleable.VectorDrawable_height, 24);
a.recycle(); a.recycle();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(String.format(Locale.ENGLISH, "<svg id=\"vector\" xmlns=\"http://www.w3.org/2000/svg\" width=\"%f\" height=\"%f\" viewBox=\"0 0 %f %f\">",
width, height, viewportWidth, viewportHeight));
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
&& (parser.getDepth() >= innerDepth && (parser.getDepth() >= innerDepth
|| type != XmlPullParser.END_TAG)) { || type != XmlPullParser.END_TAG)) {
@@ -60,20 +63,31 @@ public class VectorDrawable extends Drawable {
fillColor & 0xFFFFFF, (fillColor >> 24) & 0xFF, strokeColor & 0xFFFFFF, (strokeColor >> 24) & 0xFF, strokeWidth, pathData)); fillColor & 0xFFFFFF, (fillColor >> 24) & 0xFF, strokeColor & 0xFFFFFF, (strokeColor >> 24) & 0xFF, strokeWidth, pathData));
} }
} }
sb.append("</svg>"); svg = sb.toString();
String svg = sb.toString(); setBounds(0, 0, (int)width, (int)height);
byte[] bytes = svg.getBytes(); }
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
if (bitmap == null || bitmap.getWidth() != right - left || bitmap.getHeight() != bottom - top) {
if (bitmap != null)
bitmap.recycle();
String s = String.format(Locale.ENGLISH, "<svg id=\"vector\" xmlns=\"http://www.w3.org/2000/svg\" width=\"%f\" height=\"%f\" viewBox=\"0 0 %f %f\">%s</svg>",
(float)(right - left), (float)(bottom - top), viewportWidth, viewportHeight, svg);
byte[] bytes = s.getBytes();
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
} }
@Override @Override
public int getIntrinsicWidth() { public int getIntrinsicWidth() {
return bitmap.getWidth(); return (int)width;
} }
@Override @Override
public int getIntrinsicHeight() { public int getIntrinsicHeight() {
return bitmap.getHeight(); return (int)height;
} }
@Override @Override