2024-03-24 15:41:39 +01:00
|
|
|
package android.graphics.drawable;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2024-07-20 22:03:15 +02:00
|
|
|
import java.util.Locale;
|
2024-03-24 15:41:39 +01:00
|
|
|
|
|
|
|
|
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 {
|
2024-04-09 18:58:05 +02:00
|
|
|
|
2024-08-05 17:10:06 +02:00
|
|
|
private Bitmap bitmap; // prevent garbage collection
|
|
|
|
|
|
2024-03-24 15:41:39 +01:00
|
|
|
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);
|
2024-07-20 22:03:15 +02:00
|
|
|
float width = a.getDimension(R.styleable.VectorDrawable_width, 24);
|
|
|
|
|
float height = a.getDimension(R.styleable.VectorDrawable_height, 24);
|
2024-03-24 15:41:39 +01:00
|
|
|
a.recycle();
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2024-07-20 22:03:15 +02:00
|
|
|
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));
|
2024-03-24 15:41:39 +01:00
|
|
|
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();
|
2024-07-20 22:03:15 +02:00
|
|
|
sb.append(String.format(Locale.ENGLISH, "<path fill=\"#%06x%02x\" stroke=\"#%06x%02x\" stroke-width=\"%f\" d=\"%s\"/>",
|
2024-03-24 15:41:39 +01:00
|
|
|
fillColor & 0xFFFFFF, (fillColor >> 24) & 0xFF, strokeColor & 0xFFFFFF, (strokeColor >> 24) & 0xFF, strokeWidth, pathData));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb.append("</svg>");
|
|
|
|
|
String svg = sb.toString();
|
|
|
|
|
byte[] bytes = svg.getBytes();
|
2024-08-05 17:10:06 +02:00
|
|
|
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
|
|
|
|
|
this.paintable = bitmap.getTexture();
|
2024-03-24 15:41:39 +01:00
|
|
|
}
|
2024-04-09 18:58:05 +02:00
|
|
|
|
2024-06-22 14:22:37 +02:00
|
|
|
@Override
|
|
|
|
|
public int getIntrinsicWidth() {
|
|
|
|
|
return 24; // FIXME
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getIntrinsicHeight() {
|
|
|
|
|
return 24; // FIXME
|
|
|
|
|
}
|
2024-03-24 15:41:39 +01:00
|
|
|
}
|