/**
* 生成适配文件
* ldpi 1dp = 0.75px 320*240
* mdpi 1dp = 1px 480*320
* hdpi 1dp = 1.5px 800*480
* xhdpi 1dp = 2px 1280*720
* xxhdpi 1dp = 3px 1920*1080
*/
public class DimensTools {
public static void main(String[] args) {
Transform();
}
public static void Transform() {
//以此文件夹下的dimens.xml文件内容为初始值参照
File file = new File("./app/src/main/res/values/dimens.xml");
BufferedReader reader = null;
StringBuilder sw_hdpi = new StringBuilder();
StringBuilder sw_xhdpi = new StringBuilder();
StringBuilder sw_xxhdpi = new StringBuilder();
StringBuilder sw_xxxhdpi = new StringBuilder();
try {
reader = new BufferedReader(new FileReader(file));
String tempString;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
if (tempString.contains("</dimen>")) {
String start = tempString.substring(0, tempString.indexOf(">") + 1);
String end = tempString.substring(tempString.lastIndexOf("<") - 2);
//截取<dimen></dimen>标签内的内容,从>右括号开始,到左括号减2,取得配置的数字
Double num = Double.parseDouble(tempString.substring(tempString.indexOf(">") + 1,
tempString.indexOf("</dimen>") - 2));
//根据不同的尺寸,计算新的值,拼接新的字符串,并且结尾处换行。
sw_hdpi.append(start).append(num * 0.55).append(end).append("\r\n");
sw_xhdpi.append(start).append(num * 1).append(end).append("\r\n");
sw_xxhdpi.append(start).append(num * 0.95).append(end).append("\r\n");
sw_xxxhdpi.append(start).append(num * 1.15).append(end).append("\r\n");
} else {
sw_hdpi.append(tempString).append("");
sw_xhdpi.append(tempString).append("");
sw_xxhdpi.append(tempString).append("");
sw_xxxhdpi.append(tempString).append("");
}
line++;
}
reader.close();
System.out.println("<!-- sw_hdpi -->");
System.out.println(sw_hdpi);
System.out.println("<!-- sw_xhdpi -->");
System.out.println(sw_xhdpi);
System.out.println("<!-- sw_xxhdpi -->");
System.out.println(sw_xxhdpi);
System.out.println("<!-- sw_xxxhdpi -->");
System.out.println(sw_xxhdpi);
sw_hdpi.append(tempString).append("");
sw_xhdpi.append(tempString).append("");
sw_xxhdpi.append(tempString).append("");
sw_xxxhdpi.append(tempString).append("");
//直接指定文件夹路径,以及文件名及格式。上来就是干!
// File w_mdpi = new File("./app/src/main/res/values-mdpi/dimens.xml");
File w_hdpi = new File("./app/src/main/res/values-hdpi/dimens.xml");
File w_xhdpi = new File("./app/src/main/res/values-xhdpi/dimens.xml");
File w_xxhdpi = new File("./app/src/main/res/values-xxhdpi/dimens.xml");
File w_xxxhdpi = new File("./app/src/main/res/values-xxxhdpi/dimens.xml");
// Make(w_mdpi);
Make(w_hdpi);
Make(w_xhdpi);
Make(w_xxhdpi);
Make(w_xxxhdpi);
//将新的内容,写入到指定的文件中去
writeFile(w_hdpi, sw_hdpi.toString());
writeFile(w_xhdpi, sw_xhdpi.toString());
writeFile(w_xxhdpi, sw_xxhdpi.toString());
writeFile(w_xxxhdpi, sw_xxxhdpi.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 写入方法
*/
public static void writeFile(File file, String text) {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
out.println(text);
} catch (IOException e) {
e.printStackTrace();
}
out.close();
}
//自定义检测生成指定文件夹下的指定文件
public static void Make(File file) {
try {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这个工具类不是我写的,但是我也不知道是谁写的,只是留在这mark一下,留着下次用,使用时只需要
在这个类里,鼠标右键,选择运行里面的main方法即可,生成对应的适配文件。可以自己调整比例