相关文章推荐
可爱的石榴  ·  Your request has been ...·  1 年前    · 
不拘小节的香蕉  ·  景区直通车 || ...·  1 年前    · 
逼格高的沙发  ·  2020年度马尾区政府工作报告_工作报告_福 ...·  1 年前    · 
跑龙套的葫芦  ·  小米12拍照重回索尼怀抱:标准版用IMX76 ...·  2 年前    · 
八块腹肌的西装  ·  请静音乐_好听的请静音乐_在线请静音乐欣赏 ...·  2 年前    · 
Code  ›  自己写一个svg转化为安卓xml的工具类开发者社区
string android xml语言 svg
https://cloud.tencent.com/developer/article/1368132
谈吐大方的楼房
2 年前
张风捷特烈

自己写一个svg转化为安卓xml的工具类

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
张风捷特烈
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > 自己写一个svg转化为安卓xml的工具类

自己写一个svg转化为安卓xml的工具类

作者头像
张风捷特烈
发布 于 2018-12-05 10:43:05
2K 0
发布 于 2018-12-05 10:43:05
举报
文章被收录于专栏: Android知识点总结

svg资源阿里巴巴矢量资源网站: http://www.iconfont.cn/ 感觉一般的svg到Android可用的xml差异有点规律,主要的就是path 秉承着 能用代码解决的问题,绝对不动手。能够靠智商解决的问题,绝对不靠体力 的大无畏精神: 写段代码批处理一下,要比一个一个在网上转换方便一些。

1.样例svg
代码语言: javascript
复制
<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg t="1540950990615" class="icon" style="" viewBox="0 0 1024 1024" version="1.1" 
    xmlns="http://www.w3.org/2000/svg" p-id="10665" 
    xmlns:xlink="http://www.w3.org/1999/xlink" width="48" height="48">
        <style type="text/css"></style>
    </defs>
    <path d="M257.22 814.53a36.46 36.46 0 0 1-25.78-62.24L471.73 512 231.44 271.71A36.46 36.46 0 0 1 283 220.15l246.77 246.73a63.83 63.83 0 0 1 0 90.2L283 803.85a36.35 36.35 0 0 1-25.78 10.68z" fill="#42494F" p-id="10666">
    </path>
    <path d="M512 814.53a36.46 36.46 0 0 1-25.78-62.24L726.47 512 486.18 271.71a36.46 36.46 0 0 1 51.56-51.56l246.77 246.73a63.66 63.66 0 0 1 0 90.2L537.75 803.85A36.35 36.35 0 0 1 512 814.53z" fill="#42494F" p-id="10667">
    </path>
</svg>
2.转化成的Android可用的xml
代码语言: javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="48dp"
        android:height="48dp"
        android:viewportWidth="1024"
        android:viewportHeight="1024">
        android:fillColor="#FF7F47"
        android:pathData="M257.22 814.53a36.46 36.46 0 0 1-25.78-62.24L471.73 512 231.44 271.71A36.46 36.46 0 0 1 283 220.15l246.77 246.73a63.83 63.83 0 0 1 0 90.2L283 803.85a36.35 36.35 0 0 1-25.78 10.68z"/>
        android:fillColor="#FF7F47"
        android:pathData="M512 814.53a36.46 36.46 0 0 1-25.78-62.24L726.47 512 486.18 271.71a36.46 36.46 0 0 1 51.56-51.56l246.77 246.73a63.66 63.66 0 0 1 0 90.2L537.75 803.85A36.35 36.35 0 0 1 512 814.53z"/>
</vector>

一、转换一个svg文件的代码:
代码语言: javascript
复制
    /**
     * 将.svg文件转换为安卓可用的.xml
     * @param file 文件路径
    public static void svg2xml(File file) {
        if (!file.exists() && file.isDirectory()) {
            return;
        FileWriter fw = null;
        FileReader fr = null;
        ArrayList<String> paths = new ArrayList<>();
        try {
            fr = new FileReader(file);
            //字符数组循环读取
            char[] buf = new char[1024];
            int len = 0;
            StringBuilder sb = new StringBuilder();
            while ((len = fr.read(buf)) != -1) {
                sb.append(new String(buf, 0, len));
            //收集所有path
            collectPaths(sb.toString(), paths);
            //拼接字符串
            StringBuilder outSb = contactStr(paths);
            //写出到磁盘
            File outFile = new File(file.getParentFile(), file.getName().substring(0, file.getName().lastIndexOf(".")) + ".xml");
            fw = new FileWriter(outFile);
            fw.write(outSb.toString());
            System.out.println("OK:" + outFile.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                if (fr != null) {
                    fr.close();
            } catch (Exception e) {
                e.printStackTrace();
     * 拼接字符串
     * @param paths
     * @return
    private static StringBuilder contactStr(ArrayList<String> paths) {
        StringBuilder outSb = new StringBuilder();
        outSb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<vector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
                "        android:width=\"48dp\"\n" +
                "        android:height=\"48dp\"\n" +
                "        android:viewportWidth=\"1024\"\n" +
                "        android:viewportHeight=\"1024\">\n");
        for (String path : paths) {
            outSb.append("    <path\n" +
                    "        android:fillColor=\"#FF7F47\"\nandroid:pathData=");
            outSb.append(path);
            outSb.append("/>");
        outSb.append("</vector>");
        return outSb;
     * 收集所有path
     * @param result
     * @param paths
    private static void collectPaths(String result, ArrayList<String> paths) {
        String[] split = result.split("<path");
        for (String s : split) {
            if (s.contains("path")) {
                int endIndex;
                if (!s.contains("fill")) {
                    endIndex = s.indexOf("p");
                } else {
                    endIndex = Math.min(s.indexOf("f"), s.indexOf("p"));
                String path = s.substring(s.indexOf("\""), endIndex);
                paths.add(path);
    }
转换一个文件夹里的所有svg图片
代码语言: javascript
复制
/**
 * 将一个文件夹里的所有svg转换为xml
 * @param filePath
public static void svg2xmlFromDir(String filePath) {
    File file = new File(filePath);
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.getName().endsWith(".svg")) {
                System.out.println(f);
                svg2xml(f);
 
推荐文章
可爱的石榴  ·  Your request has been blocked. This could be due to several reasons.
1 年前
不拘小节的香蕉  ·  景区直通车 || 好消息!欢迎乘坐“沈阳兴隆温泉城景区直通车”
1 年前
逼格高的沙发  ·  2020年度马尾区政府工作报告_工作报告_福州市马尾区人民政府门户网站
1 年前
跑龙套的葫芦  ·  小米12拍照重回索尼怀抱:标准版用IMX766,Pro版用IMX707_传感器_手机_Reno
2 年前
八块腹肌的西装  ·  请静音乐_好听的请静音乐_在线请静音乐欣赏 - 请静音乐网 360恒牛网 FX9K网站 ee分类目录 辉煌空包 97空包 海岸线文学网桠邈肯瞻,占蕈釜钰,真鹄菠箸,狍帜奸路,社禹窬酷,啕球封喷,禁埽噎崎,腺凼份蕃
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号