package com.basic.security.utils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import java.io.File;
|
import java.io.FileReader;
|
import java.io.FileWriter;
|
import java.text.DecimalFormat;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
public class ResolutionUtil {
|
public static List<Size> sizeList = new ArrayList<>();
|
static DecimalFormat df = new DecimalFormat("#.#");
|
|
private static void generateLayXLayY() {
|
File dir = new File("E:\\workspace\\Security_yuyin_landscape_dual\\app\\src\\main\\res\\values");
|
for (Size size : sizeList) {
|
File layXY = new File(dir, "lay_x_y_" + size.width + "_" + size.height + ".xml");
|
if (layXY.exists()) {
|
layXY.delete();
|
}
|
try {
|
FileWriter fw = new FileWriter(layXY);
|
fw.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
|
"<resources>");
|
for (int i = 1; i <= 1280; i++) {
|
String hI = "" + df.format(size.width * 1.0 / 1280.0 * i) + "px";
|
fw.write("<dimen name=\"h" + size.width + "_" + size.height + "_" + i + "\">" + hI + "</dimen>\r\n");
|
}
|
for (int i = 1; i <= 800; i++) {
|
String hI = "" + df.format(size.height * 1.0 / 800.0 * i) + "px";
|
fw.write("<dimen name=\"w" + +size.width + "_" + size.height + "_" + i + "\">" + hI + "</dimen>\r\n");
|
}
|
fw.write("\n" +
|
"</resources>");
|
fw.flush();
|
fw.close();
|
System.out.println("ResolutionUtil.generateLayXLayY " + layXY.getAbsolutePath());
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
public static void main(String[] args) {
|
{
|
sizeList.add(new Size(1920, 1080));
|
sizeList.add(new Size(1280, 800));
|
sizeList.add(new Size(2560, 1440));
|
sizeList.add(new Size(2560, 1600));
|
}
|
// generateLayXLayY();
|
generateLayout();
|
// String str = "<item name=\"android:layout_height\">@dimen/h60</item>";
|
// str = str.substring(str.indexOf("@dimen/h") + "@dimen/h".length());
|
// String value = "";
|
// for (int i = 0; i < str.length(); i++) {
|
// if (str.charAt(i) == '"' || str.charAt(i) == '<') {
|
// break;
|
// }
|
// value += str.charAt(i);
|
// }
|
// System.out.println("ResolutionUtil.main " + value);
|
}
|
|
private static void generateLayout() {
|
try {
|
File dir = new File("E:\\workspace\\Security_yuyin_landscape_dual\\app\\src\\main\\res\\layout");
|
File resolutionAdaptation = new File("E:\\workspace\\Security_yuyin_landscape_dual\\app\\src\\main\\java\\com\\basic\\security\\utils\\ResolutionAdaptation.java");
|
FileWriter resolutionAdaptationFW = new FileWriter(resolutionAdaptation);
|
File[] layoutFiles = dir.listFiles();
|
resolutionAdaptationFW.write("package com.basic.security.utils;\n" +
|
"\n" +
|
"import com.basic.security.yuyin.indoor.R;\n" +
|
"\n" +
|
"public class ResolutionAdaptation {\n");
|
for (File layoutFile : layoutFiles) {
|
if (layoutFile.getName().startsWith("z_")) {
|
continue;
|
}
|
resolutionAdaptationFW.write("public static int " + layoutFile.getName().replace(".xml", "") + "(){\n");
|
for (Size size : sizeList) {
|
File layXY = new File(dir, "z_" + layoutFile.getName().replace(".xml", "") + "_" + size.width + "_" + size.height + ".xml");
|
if (layXY.exists()) {
|
layXY.delete();
|
}
|
try {
|
List<String> lines = IOUtils.readLines(new FileReader(layoutFile));
|
for (int i = 0; i < lines.size(); i++) {
|
String line = lines.get(i);
|
line = line.replace("@dimen/h", "@dimen/h" + size.width + "_" + size.height + "_");
|
line = line.replace("@dimen/w", "@dimen/w" + size.width + "_" + size.height + "_");
|
lines.set(i, line);
|
}
|
FileWriter fw = new FileWriter(layXY);
|
IOUtils.writeLines(lines, "\r\n", fw);
|
fw.flush();
|
fw.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
// resolutionAdaptationFW.write("if (LayoutUtil.getRealWidth()=="+size.width+" && LayoutUtil.getRealHeight()=="+size.height+"){\n");
|
// resolutionAdaptationFW.write(" return R.layout."+layXY.getName().replace(".xml", "")+";\n");
|
// resolutionAdaptationFW.write("}\n");
|
}
|
resolutionAdaptationFW.write("return R.layout." + layoutFile.getName().replace(".xml", "") + ";\n");
|
resolutionAdaptationFW.write("}\n");
|
}
|
for (int i = 1; i <= 1280; i++) {
|
resolutionAdaptationFW.write("public static int h" + i + "(){\n");
|
for (Size size : sizeList) {
|
// resolutionAdaptationFW.write("if (LayoutUtil.getRealWidth()=="+size.width+" && LayoutUtil.getRealHeight()=="+size.height+"){\n");
|
// resolutionAdaptationFW.write(" return R.dimen.h"+size.width+"_"+size.height+"_"+i+";\n");
|
// resolutionAdaptationFW.write("}\n");
|
}
|
resolutionAdaptationFW.write("return R.dimen.h" + i + ";\n");
|
resolutionAdaptationFW.write("}\n");
|
}
|
for (int i = 1; i <= 800; i++) {
|
resolutionAdaptationFW.write("public static int w" + i + "(){\n");
|
for (Size size : sizeList) {
|
// resolutionAdaptationFW.write("if (LayoutUtil.getRealWidth()=="+size.width+" && LayoutUtil.getRealHeight()=="+size.height+"){\n");
|
// resolutionAdaptationFW.write(" return R.dimen.w"+size.width+"_"+size.height+"_"+i+";\n");
|
// resolutionAdaptationFW.write("}\n");
|
}
|
resolutionAdaptationFW.write("return R.dimen.w" + i + ";\n");
|
resolutionAdaptationFW.write("}\n");
|
}
|
resolutionAdaptationFW.write("}\n");
|
resolutionAdaptationFW.flush();
|
resolutionAdaptationFW.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public static class Size {
|
int width;
|
int height;
|
|
public Size(int width, int height) {
|
this.width = width;
|
this.height = height;
|
}
|
}
|
}
|