屏幕适配解决方案
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
* Created by phoenix
*/
public class MakeXml {
// private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\";
private static final String rootPath = "\\src\\main\\res\\values-{0}x{1}\\";
private final static float dw = 1080f;
private final static float dh = 1920f;
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(240, 320);
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(1080, 1812);
makeString(1080, 1920);
makeString(1440, 2560);
// getCurrentDirectory();
}
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 < 1080; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));
}
sb.append(WTemplate.replace("{0}", "1080").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 < 1920; i++) {
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));
}
sb2.append(HTemplate.replace("{0}", "1920").replace("{1}", h + ""));
sb2.append("</resources>");
String path = getCurrentDirectory() + 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;
}
private static String getCurrentDirectory() {
try {
File file = new File(System.getProperty("user.dir"));
System.out.println("file.getAbsolutePath():\n" + file.getAbsolutePath());
// System.out.println("file.getCanonicalPath():\n" + file.getCanonicalPath());
// System.out.println("file.getParent():\n" + file.getParent());
// System.out.println("file.getParentFile().getAbsolutePath():\n" + file.getParentFile().getAbsolutePath());
// System.out.println("file.getParentFile().getCanonicalPath():\n" + file.getParentFile().getCanonicalPath());
return file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
/**
* Created by phoenix
*/
public class MakeXml {
// private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\";
private static final String rootPath = "\\src\\main\\res\\values-{0}x{1}\\";
private final static float dw = 1080f;
private final static float dh = 1920f;
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(240, 320);
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(1080, 1812);
makeString(1080, 1920);
makeString(1440, 2560);
// getCurrentDirectory();
}
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 < 1080; i++) {
sb.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));
}
sb.append(WTemplate.replace("{0}", "1080").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 < 1920; i++) {
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));
}
sb2.append(HTemplate.replace("{0}", "1920").replace("{1}", h + ""));
sb2.append("</resources>");
String path = getCurrentDirectory() + 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;
}
private static String getCurrentDirectory() {
try {
File file = new File(System.getProperty("user.dir"));
System.out.println("file.getAbsolutePath():\n" + file.getAbsolutePath());
// System.out.println("file.getCanonicalPath():\n" + file.getCanonicalPath());
// System.out.println("file.getParent():\n" + file.getParent());
// System.out.println("file.getParentFile().getAbsolutePath():\n" + file.getParentFile().getAbsolutePath());
// System.out.println("file.getParentFile().getCanonicalPath():\n" + file.getParentFile().getCanonicalPath());
return file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
1.cmd运行 我是将这个文件放到桌面上 所以应该先切换到桌面上
2.javac MakeXml.java 生成class文件
3.java MakeXml 批量生成多尺寸布局文件
4.注意 如果控制台出现乱码 需要将文件另存为 修改编码