a
554325746@qq.com
2019-12-24 570a73851c26d810c2597596a8acc8a8d4cde211
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
        }
    }
}