-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainAndroidResUtil
170 lines (120 loc) · 3.98 KB
/
MainAndroidResUtil
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
168
169
170
package com.chenkui.androidscreen;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainAndroidResUtil {
public static void main(String[] args) {
new MainAndroidResUtil().start();
}
public void start(){
setAndroidScreen();
File resFile = new File("./res");
if (resFile.exists()) {
System.out.println("当前目录存在同名文件夹,请处理");
} else {
resFile.mkdir();
System.out.println("创建成功");
}
System.out.println(resFile.getAbsolutePath());
// 设置以480*800为计算标准
// 根文件创建成功,则创建其他文件夹
File file;
Iterator<String> iterator = androidSreenSizeAll.iterator();
while (iterator.hasNext()) {
String dirName = iterator.next();
System.out.println("dirName ==== " + dirName);
file = new File("./res/values-" + dirName);
if (!file.exists()) {
file.mkdir();
String desValue = dirName;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(desValue);
String numValueOne = "";
String numValueTwo = "";
if (m.find()) {
numValueOne = m.group(0);
if (m.find()) {
numValueTwo = m.group(0);
}
}
System.out.println("numValueOne === " + numValueOne);
System.out.println("numValueTwo === " + numValueTwo);
// 获取最小值
int num;
int intNumOne = Integer.parseInt(numValueOne);
int intNumTwo = Integer.parseInt(numValueTwo);
System.out.println("------------------numValueTwo === "
+ numValueTwo);
if (intNumOne > intNumTwo) {
num = intNumTwo;
} else {
num = intNumOne;
}
// 获取原始效果尺寸480x800,1280x800
int intSrcSize = Integer.parseInt("480");
// int intSrcSize = Integer.parseInt("800");
float scale = (float) ((num * 1.0) / intSrcSize);
File dimensFile = new File(file.getAbsoluteFile()
+ "/dimens.xml");
try {
outContent(dimensFile, scale);
} catch (IOException e) {
System.out.println("生成文件错误,请稍后重试");
return;
}
}
}
}
public void outContent(File desFile, float scale) throws IOException {
BufferedWriter bw = null;
FileOutputStream fos;
fos = new FileOutputStream(desFile);
bw = new BufferedWriter(new OutputStreamWriter(fos));
String hear = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
bw.write(hear + "\n");
String tag = "<resources>";
bw.write(tag + "\n");
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
for (int i = 1; i < 600; i++) {
float pxValue = scale * i;
// double d = 3.14159;
String strPXValue = df.format(pxValue);
String contentLine = "<dimen name=\"x" + i + "\">" + strPXValue
+ "px</dimen>" + "\n" + "<dimen name=\"_x" + i + "\">-"
+ strPXValue + "px</dimen>" + "\n";
bw.write(contentLine);
}
String end = "</resources>" + "\n";
bw.write(end);
bw.close();
fos.close();
}
LinkedList<String> androidSreenSizeAll = new LinkedList<String>();
public void setAndroidScreen() {
androidSreenSizeAll.add("hdpi-960x540");
androidSreenSizeAll.add("hdpi-1024x600");
androidSreenSizeAll.add("hdpi-1280x720");
androidSreenSizeAll.add("ldpi-400x240");
androidSreenSizeAll.add("ldpi-480x320");
androidSreenSizeAll.add("mdpi-800x480");
androidSreenSizeAll.add("mdpi-800x600");
androidSreenSizeAll.add("mdpi-1024x600");
androidSreenSizeAll.add("mdpi-1024x768");
androidSreenSizeAll.add("mdpi-1280x720");
androidSreenSizeAll.add("xhdpi-960x640");
androidSreenSizeAll.add("xhdpi-1184x720");
androidSreenSizeAll.add("xhdpi-1280x720");
androidSreenSizeAll.add("xhdpi-1280x800");
androidSreenSizeAll.add("xhdpi-1776x1080");
// oppo r9 ,MEIZU MX6,
androidSreenSizeAll.add("xhdpi-1920x1080");
// 三星S6
androidSreenSizeAll.add("xhdpi-2560x1440");
}
}