http://blog.youkuaiyun.com/lmj623565791/article/details/45460089;
本文基础:【张鸿洋的博客】
在这个基础上自己稍微修改了一下,原文是以320×480这个分辨率为基础来生成的values文件。我在基础上增加了完全按照百分比的形式生成values文件。
增加两个变量,用来生成对应values文件。
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";
private final static String WPercent = "<dimen name=\"w{0}\">{1}px</dimen>\n";
private final static String HPercent = "<dimen name=\"h{0}\">{1}px</dimen>\n";
修改了这个生成文件
private void generateXmlFile(int w, int h) {
StringBuffer sbForWidth = new StringBuffer();
sbForWidth.append("<!--?xml version=\"1.0\" encoding=\"utf-8\"?-->\n");
sbForWidth.append("<resources>");
float cellw = w * 1.0f / baseW;
System.out.println("width : " + w + "," + baseW + "," + cellw);
for (int i = 1; i < baseW; i++) {
sbForWidth.append(WTemplate.replace("{0}", i + "").replace("{1}",
change(cellw * i) + ""));
}
sbForWidth.append(WTemplate.replace("{0}", baseW + "").replace("{1}",
w + ""));
for (int i = 1; i < 1000; i++) {
if (i % 10 == 0) {
sbForWidth.append(WPercent.replace("{0}", i / 10 + "").replace("{1}",
change(i * 0.001f * w) + ""));
}else{
sbForWidth.append(WPercent.replace("{0}", i / 10 + "." + i % 10).replace("{1}",
change(i * 0.001f * w) + ""));
}
}
sbForWidth.append(WPercent.replace("{0}", "100.0").replace("{1}",
change(1f * w) + ""));
sbForWidth.append("</resources>");
StringBuffer sbForHeight = new StringBuffer();
sbForHeight.append("<!--?xml version=\"1.0\" encoding=\"utf-8\"?-->\n");
sbForHeight.append("<resources>");
float cellh = h * 1.0f / baseH;
System.out.println("height : " + h + "," + baseH + "," + cellh);
for (int i = 1; i < baseH; i++) {
sbForHeight.append(HTemplate.replace("{0}", i + "").replace("{1}",
change(cellh * i) + ""));
}
sbForHeight.append(HTemplate.replace("{0}", baseH + "").replace("{1}",
h + ""));
for (int i = 1; i < 1000; i++) {
if (i % 10 == 0) {
sbForHeight.append(HPercent.replace("{0}", i / 10 + "").replace("{1}",
change(i * 0.001f * h) + ""));
}else {
sbForHeight.append(HPercent.replace("{0}", i / 10 + "." + i % 10).replace("{1}",
change(i * 0.001f * h) + ""));
}
}
sbForHeight.append(HPercent.replace("{0}", "100").replace("{1}",
change(1f * h) + ""));
sbForHeight.append("</resources>");
File fileDir = new File(dirStr + File.separator
+ VALUE_TEMPLATE.replace("{0}", h + "")//
.replace("{1}", w + ""));
fileDir.mkdir();
File layxFile = new File(fileDir.getAbsolutePath(), "lay_x.xml");
File layyFile = new File(fileDir.getAbsolutePath(), "lay_y.xml");
try {
PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
pw.print(sbForWidth.toString());
pw.close();
pw = new PrintWriter(new FileOutputStream(layyFile));
pw.print(sbForHeight.toString());
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}