1. 程式人生 > >自己寫一個svg轉化為安卓xml的工具類

自己寫一個svg轉化為安卓xml的工具類

svg資源阿里巴巴向量資源網站:http://www.iconfont.cn/
感覺一般的svg到Android可用的xml差異有點規律,主要的就是path
秉承著能用程式碼解決的問題,絕對不動手。能夠靠智商解決的問題,絕對不靠體力的大無畏精神:
寫段程式碼批處理一下,要比一個一個在網上轉換方便一些。

1.樣例svg
<?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">

    <defs>
        <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
<?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">
    <path
        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"/>
    <path
        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檔案的程式碼:

    /**
     * 將.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圖片
/**
 * 將一個資料夾裡的所有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);
            }
        }
    } else {
        svg2xml(file);
    }
}
將xml放在drawable目錄下,就可以當資原始檔用了,大小顏色都可以操作
9414344-aecddc25c9e05871.png 使用效果.png