package com;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
/**
* 快速生成适配工具类
*/
public class DimenTool {
private static List<String> targetFileList;
private static String startFlag, stopFlag;
public static void gen() {
//以此文件夹下的dimens.xml文件内容为初始值参照
File fromFile = new File("./host/res/values-sw200dp/dimens.xml");
startFlag = "<!-- add for adjust pm start";
stopFlag = "<!-- add for adjust pm end -->";
double rotate = 0.2;
targetFileList = new ArrayList<>();
targetFileList.add("./host/res/values-sw250dp/dimens.xml");
targetFileList.add("./host/res/values-sw300dp/dimens.xml");
targetFileList.add("./host/res/values-sw350dp/dimens.xml");
targetFileList.add("./host/res/values-sw400dp/dimens.xml");
targetFileList.add("./host/res/values-sw450dp/dimens.xml");
targetFileList.add("./host/res/values-sw500dp/dimens.xml");
targetFileList.add("./host/res/values-sw530dp/dimens.xml");
targetFileList.add("./host/res/values-sw600dp/dimens.xml");
targetFileList.add("./host/res/values-sw620dp/dimens.xml");
targetFileList.add("./host/res/values-sw700dp/dimens.xml");
targetFileList.add("./host/res/values-sw750dp/dimens.xml");
targetFileList.add("./host/res/values-sw800dp/dimens.xml");
targetFileList.add("./host/res/values-sw850dp/dimens.xml");
targetFileList.add("./host/res/values-sw900dp/dimens.xml");
targetFileList.add("./host/res/values-sw941dp/dimens.xml");
BufferedReader reader = null;
StringBuilder appendString = null;
for (int i = 0; i < targetFileList.size(); i++) {
String targetFile = targetFileList.get(i);
appendString = new StringBuilder();
try {
reader = new BufferedReader(new FileReader(new File(targetFile)));
String tempString;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//起始行数过滤
if (tempString.contains(startFlag) || tempString.contains("</resources>")) {
break;
}
appendString.append(tempString).append("\r\n");
}
reader.close();
System.out.println("<!-- read target finish -->");
System.out.println(appendString);
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("生成不同分辨率:");
reader = new BufferedReader(new FileReader(fromFile));
String tempString;
boolean fromThisLine = false;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
//起始行数过滤
if (!fromThisLine) {
fromThisLine = tempString.contains(startFlag);
if (fromThisLine) {
appendString.append(tempString);
}
continue;
}
if (tempString.contains("</dimen>")) {
//tempString = tempString.replaceAll(" ", "");
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));
//根据不同的尺寸,计算新的值,拼接新的字符串,并且结尾处换行。
DecimalFormat df = new DecimalFormat(".00");
double dou = num + num * rotate * (i + 1);
appendString.append("\r\n").append(start).append(df.format(dou)).append(end);
} else {
appendString.append("\r\n").append(tempString);
}
}
reader.close();
System.out.println("<!-- Finish -->");
System.out.println(appendString);
writeFile(targetFile, appendString.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 写入方法
*/
public static void writeFile(String 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 main(String[] args) {
gen();
}
}