初学Android适配——生成适配文件的工具类MakeXml

来源:互联网 发布:淘宝提交大学生认证 编辑:程序博客网 时间:2024/05/29 17:42
package sliding.pz.com.makexml;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.PrintWriter;/** * Created by lenovo on 2016/12/2. */public class MakeXml {    // 生成地址 C盘layoutroot目录下    private final static String rootPath = "C:\\layoutroot\\values-{0}x{1}\\";    /**     * 设置基准分辨率     * 一般标注按照多大的图标,这里我们就设置多大尺寸     */    private final static float dw = 720f;    private final static float dh = 1280f;    private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";    private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";    // 手机分辨率    public static void main(String [] args){        makeString(320, 480);        makeString(480, 800);        makeString(480, 854);        makeString(540, 960);        makeString(600, 1024);        makeString(720, 1184);        makeString(720, 1196);        makeString(720, 1280);        makeString(768, 1024);        makeString(800, 1280);        makeString(900, 1440);        makeString(1080, 1812);        makeString(1080, 1920);        makeString(1440, 2560);    }    public static void makeString(int w, int h) {        StringBuffer sb = new StringBuffer();        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");        sb.append("<resources>");        float cellw = w / dw;        for (int i = 1; i < dw; i++) {            sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));        }        sb.append(WTemplate.replace("{0}", "720").replace("{1}", w + ""));        sb.append("</resources>");        StringBuffer sb2 = new StringBuffer();        sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");        sb2.append("<resources>");        float cellh = h / dh;        for (int i = 1; i < dh; i++) {            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));        }        sb2.append(HTemplate.replace("{0}", "1280").replace("{1}", h + ""));        sb2.append("</resources>");        String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");        File rootFile = new File(path);        if (!rootFile.exists()) {            rootFile.mkdirs();        }        File layxFile = new File(path + "lay_x.xml");        File layyFile = new File(path + "lay_y.xml");        try {            PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));            pw.print(sb.toString());            pw.close();            pw = new PrintWriter(new FileOutputStream(layyFile));            pw.print(sb2.toString());            pw.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        }    }    public static float change(float a) {        int temp = (int) (a * 100);        return temp / 100f;    }}
0 0
原创粉丝点击