- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateValueFiles.java
More file actions
Latest commit
167 lines (138 loc) · 5.63 KB
/
Copy pathGenerateValueFiles.java
File metadata and controls
167 lines (138 loc) · 5.63 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
packagecom.hongfans.generatevaluefiles;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.PrintWriter;
/**
* Created by zhy on 15/5/3.
*/
publicclassGenerateValueFiles {
privateintmBaseW; // 基准宽
privateintmBaseH; // 基准高
privateStringdirStr = "./res"; // 生成的文件目录
privatefinalstaticStringWTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
privatefinalstaticStringHTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
/**
* {0}-HEIGHT
*/
privatefinalstaticStringVALUE_TEMPLATE = "values-{0}x{1}";
// private static final String SUPPORT_DIMENSION = "320,480;480,640;400,648;480,722;480,800;600,800;480,806;480,854;480,960;540,960;640,960;600,1024;768,1024;720,1080;640,1136;480,1148;400,1180;720,1184;720,1196;480,1280;720,1280;800,1280;1080,1774;1080,1812;1080,1920;1536,2048;1440,2560;";
privatestaticfinalStringSUPPORT_DIMENSION = "540,960;600,1024;720,1280;800,1280;1080,1774;1080,1812;1080,1920;1536,2048;1440,2560;";
privateStringmSupportStr = SUPPORT_DIMENSION;
publicGenerateValueFiles(intbaseX, intbaseY, StringsupportStr) {
mBaseW = baseX;
mBaseH = baseY;
if (!mSupportStr.contains(baseX + "," + baseY)) {
mSupportStr += baseX + "," + baseY + ";";
}
mSupportStr += validateInput(supportStr);
print("supportStr " + supportStr);
Filedir = newFile(dirStr);
if (!dir.exists()) {
dir.mkdir();
}
print("生成目录 " + dir.getAbsoluteFile());
}
publicstaticvoidmain(String[] args) {
intbaseW = 640;// 640x1136
intbaseH = 1136;
Stringaddition = "";
try {
// 命令行输入参数
if (args.length >= 3) {
baseW = Integer.parseInt(args[0]);
baseH = Integer.parseInt(args[1]);
addition = args[2];
} elseif (args.length >= 2) {
baseW = Integer.parseInt(args[0]);
baseH = Integer.parseInt(args[1]);
} elseif (args.length >= 1) {
addition = args[0];
}
} catch (NumberFormatExceptione) {
printErr("right input params : java -jar xxx.jar width height w,h_w,h_..._w,h;");
e.printStackTrace();
System.exit(-1);
}
newGenerateValueFiles(baseW, baseH, addition).generate();
}
/**
* @param supportStr w,h_...w,h;
* @return
*/
privateStringvalidateInput(StringsupportStr) {
StringBuildersb = newStringBuilder();
String[] values = supportStr.split("_");
intw;
inth;
String[] wh;
for (Stringval : values) {
try {
if (val == null || val.trim().length() == 0) {
continue;
}
wh = val.split(",");
w = Integer.parseInt(wh[0]);
h = Integer.parseInt(wh[1]);
} catch (Exceptione) {
printErr("skip invalidate params : w,h = " + val);
continue;
}
sb.append(w + "," + h + ";");
}
returnsb.toString();
}
privatevoidgenerate() {
String[] vals = mSupportStr.split(";");
for (Stringval : vals) {
String[] wh = val.split(",");
generateXmlFile(Integer.parseInt(wh[0]), Integer.parseInt(wh[1]));
}
}
privatevoidgenerateXmlFile(intw, inth) {
StringBuildersbForWidth = newStringBuilder();
sbForWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\t<resources>\n");
floatcellw = w * 1.0f / mBaseW;
print("generateXmlFile cellw " + cellw);
for (inti = 1; i < mBaseW; i++) {
sbForWidth.append(WTemplate.replace("{0}", i + "").replace("{1}", change(cellw * i) + ""));
}
sbForWidth.append(WTemplate.replace("{0}", mBaseW + "").replace("{1}", w + ""));
sbForWidth.append("</resources>");
StringBuildersbForHeight = newStringBuilder();
sbForHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\t<resources>\n");
floatcellh = h * 1.0f / mBaseH;
print("height : " + h + "," + mBaseH + "," + cellh);
for (inti = 1; i < mBaseH; i++) {
sbForHeight.append(HTemplate.replace("{0}", i + "").replace("{1}", change(cellh * i) + ""));
}
sbForHeight.append(HTemplate.replace("{0}", mBaseH + "").replace("{1}", h + ""));
sbForHeight.append("</resources>");
FilefileDir = newFile(dirStr + File.separator + VALUE_TEMPLATE.replace("{0}", h + "").replace("{1}", w + ""));
fileDir.mkdir();
FilelayxFile = newFile(fileDir.getAbsolutePath(), "lay_x.xml");
// File layxFile = new File(fileDir.getAbsolutePath(), "dimens.xml");
FilelayyFile = newFile(fileDir.getAbsolutePath(), "lay_y.xml");
try {
PrintWriterpw = newPrintWriter(newFileOutputStream(layxFile));
pw.print(sbForWidth.toString());
pw.close();
pw = newPrintWriter(newFileOutputStream(layyFile));
pw.print(sbForHeight.toString());
pw.close();
} catch (FileNotFoundExceptione) {
e.printStackTrace();
}
}
// 保留两位小数
publicstaticfloatchange(floata) {
inttemp = (int) (a * 100);
returntemp / 100f;
}
publicstaticvoidprint(Stringmsg) {
System.out.println(msg);
}
publicstaticvoidprintErr(Stringmsg) {
System.err.println(msg);
}
}