Skip to content

Commit 3621899

Browse files
committed
android屏幕适配生成代码 测试与整理
1 parent f3b7c44 commit 3621899

6 files changed

Lines changed: 300 additions & 162 deletions

File tree

‎.idea/vcs.xml‎

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎JavaTest_Zone/JavaTest_Zone.iml‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="EclipseModuleManager">
4+
<libelement value="jar://$MODULE_DIR$/lib/commons-beanutils.jar!/" />
5+
<libelement value="jar://$MODULE_DIR$/lib/commons-collections-3.0.jar!/" />
6+
<libelement value="jar://$MODULE_DIR$/lib/commons-lang-2.1.jar!/" />
7+
<libelement value="jar://$MODULE_DIR$/lib/commons-logging.jar!/" />
8+
<libelement value="jar://$MODULE_DIR$/lib/ezmorph-1.0.2.jar!/" />
9+
<libelement value="jar://$MODULE_DIR$/lib/json-lib-1.1-jdk13.jar!/" />
10+
<libelement value="jar://$MODULE_DIR$/lib/xom-1.0d10.jar!/" />
11+
<libelement value="jar://$MODULE_DIR$/lib/gson-2.3.1.jar!/" />
12+
<libelement value="jar://$MODULE_DIR$/lib/gson-2.3.1-sources.jar!/" />
13+
<libelement value="jar://$MODULE_DIR$/lib/41d75b54-56a1-31fc-aa8e-af7c6b8b9ed1.jar!/" />
14+
<libelement value="jar://$MODULE_DIR$/lib/53ffafec-781c-3146-b362-86017eb0ad06.jar!/" />
15+
<libelement value="jar://$MODULE_DIR$/lib/9333c93a-2e60-364d-ab9f-78ca7ab32ef9.jar!/" />
16+
<src_description expected_position="0">
17+
<src_folder value="file://$MODULE_DIR$/src" expected_position="0" />
18+
</src_description>
19+
</component>
20+
<component name="NewModuleRootManager" inherit-compiler-output="false">
21+
<output url="file://$MODULE_DIR$/bin" />
22+
<exclude-output />
23+
<content url="file://$MODULE_DIR$">
24+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
25+
</content>
26+
<orderEntry type="sourceFolder" forTests="false" />
27+
<orderEntry type="module-library">
28+
<library name="commons-beanutils.jar">
29+
<CLASSES>
30+
<root url="jar://$MODULE_DIR$/lib/commons-beanutils.jar!/" />
31+
</CLASSES>
32+
<JAVADOC />
33+
<SOURCES />
34+
</library>
35+
</orderEntry>
36+
<orderEntry type="module-library">
37+
<library name="commons-collections-3.0.jar">
38+
<CLASSES>
39+
<root url="jar://$MODULE_DIR$/lib/commons-collections-3.0.jar!/" />
40+
</CLASSES>
41+
<JAVADOC />
42+
<SOURCES />
43+
</library>
44+
</orderEntry>
45+
<orderEntry type="module-library">
46+
<library name="commons-lang-2.1.jar">
47+
<CLASSES>
48+
<root url="jar://$MODULE_DIR$/lib/commons-lang-2.1.jar!/" />
49+
</CLASSES>
50+
<JAVADOC />
51+
<SOURCES />
52+
</library>
53+
</orderEntry>
54+
<orderEntry type="module-library">
55+
<library name="commons-logging.jar">
56+
<CLASSES>
57+
<root url="jar://$MODULE_DIR$/lib/commons-logging.jar!/" />
58+
</CLASSES>
59+
<JAVADOC />
60+
<SOURCES />
61+
</library>
62+
</orderEntry>
63+
<orderEntry type="module-library">
64+
<library name="ezmorph-1.0.2.jar">
65+
<CLASSES>
66+
<root url="jar://$MODULE_DIR$/lib/ezmorph-1.0.2.jar!/" />
67+
</CLASSES>
68+
<JAVADOC />
69+
<SOURCES />
70+
</library>
71+
</orderEntry>
72+
<orderEntry type="module-library">
73+
<library name="json-lib-1.1-jdk13.jar">
74+
<CLASSES>
75+
<root url="jar://$MODULE_DIR$/lib/json-lib-1.1-jdk13.jar!/" />
76+
</CLASSES>
77+
<JAVADOC />
78+
<SOURCES />
79+
</library>
80+
</orderEntry>
81+
<orderEntry type="module-library">
82+
<library name="xom-1.0d10.jar">
83+
<CLASSES>
84+
<root url="jar://$MODULE_DIR$/lib/xom-1.0d10.jar!/" />
85+
</CLASSES>
86+
<JAVADOC />
87+
<SOURCES />
88+
</library>
89+
</orderEntry>
90+
<orderEntry type="module-library">
91+
<library name="gson-2.3.1.jar">
92+
<CLASSES>
93+
<root url="jar://$MODULE_DIR$/lib/gson-2.3.1.jar!/" />
94+
</CLASSES>
95+
<JAVADOC />
96+
<SOURCES>
97+
<root url="jar://$MODULE_DIR$/lib/gson-2.3.1-sources.jar!/" />
98+
</SOURCES>
99+
</library>
100+
</orderEntry>
101+
<orderEntry type="module-library">
102+
<library name="gson-2.3.1-sources.jar">
103+
<CLASSES>
104+
<root url="jar://$MODULE_DIR$/lib/gson-2.3.1-sources.jar!/" />
105+
</CLASSES>
106+
<JAVADOC />
107+
<SOURCES />
108+
</library>
109+
</orderEntry>
110+
<orderEntry type="module-library">
111+
<library name="41d75b54-56a1-31fc-aa8e-af7c6b8b9ed1.jar">
112+
<CLASSES>
113+
<root url="jar://$MODULE_DIR$/lib/41d75b54-56a1-31fc-aa8e-af7c6b8b9ed1.jar!/" />
114+
</CLASSES>
115+
<JAVADOC />
116+
<SOURCES />
117+
</library>
118+
</orderEntry>
119+
<orderEntry type="module-library">
120+
<library name="53ffafec-781c-3146-b362-86017eb0ad06.jar">
121+
<CLASSES>
122+
<root url="jar://$MODULE_DIR$/lib/53ffafec-781c-3146-b362-86017eb0ad06.jar!/" />
123+
</CLASSES>
124+
<JAVADOC />
125+
<SOURCES />
126+
</library>
127+
</orderEntry>
128+
<orderEntry type="module-library">
129+
<library name="9333c93a-2e60-364d-ab9f-78ca7ab32ef9.jar">
130+
<CLASSES>
131+
<root url="jar://$MODULE_DIR$/lib/9333c93a-2e60-364d-ab9f-78ca7ab32ef9.jar!/" />
132+
</CLASSES>
133+
<JAVADOC />
134+
<SOURCES />
135+
</library>
136+
</orderEntry>
137+
<orderEntry type="inheritedJdk" />
138+
<orderEntry type="module" module-name="JavaLib_Zone" />
139+
<orderEntry type="module-library">
140+
<library name="junit4">
141+
<CLASSES>
142+
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.12.jar!/" />
143+
</CLASSES>
144+
<JAVADOC />
145+
<SOURCES />
146+
</library>
147+
</orderEntry>
148+
</component>
149+
</module>

‎JavaTest_Zone/src/适配/DPI.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package 适配;
2+
3+
public enum DPI{
4+
ldpi("values-ldpi",0.75F) ,mdpi("values-mdpi",1.0F),hdpi("values-hdpi",1.5F),xhdpi("values-xhdpi",2F),xxhdpi("values-xxhdpi",3F);
5+
// 270*480->120dp 0.75 360*640->160dp 1 480*960->240dp 1.5 720*1280->320dp 2 1080*1920->480dp 3
6+
public String fileName;
7+
public float radio;
8+
public static DPI standardDpi;
9+
DPI(String fileName,float radio){
10+
this.fileName=fileName;
11+
this.radio=radio;
12+
};
13+
public float dx2dp(int dx){
14+
float resultDp=dx/radio;//转成 dx 通过DPI比率转成 dp
15+
if(standardDpi==null)
16+
throw new NullPointerException("standardDpi may be null");
17+
resultDp= resultDp*radio/standardDpi.radio;//在通过参考图的DPI进行对比 缩放;
18+
return change(resultDp);
19+
}
20+
public float dx2sp(int dx){
21+
return dx2dp(dx);
22+
}
23+
//保留两位小数
24+
public static float change(float a) {
25+
return Math.round( a * 100 ) / 100.0F;
26+
}
27+
}
Lines changed: 80 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,81 @@
1-
package 适配;
2-
import java.io.File;
3-
import java.io.FileNotFoundException;
4-
import java.io.FileOutputStream;
5-
import java.io.PrintWriter;
6-
7-
//values-1024x600... 适配
8-
public class MakeXml2 {
9-
10-
private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\";
11-
12-
private final static float dw = 720f;//不当成像素 当成多少份
13-
private final static float dh = 1280f;
14-
15-
private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
16-
private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
17-
18-
public static void main(String[] args) {
19-
makeFile(320, 480);//mdpi
20-
makeFile(480,800);
21-
makeFile(480, 854);//hdpi
22-
makeFile(540, 960);
23-
makeFile(600, 1024);
24-
makeFile(720, 1184);
25-
makeFile(720, 1196);
26-
makeFile(720, 1280);//xdpi
27-
makeFile(768, 1024);
28-
makeFile(800, 1280);
29-
makeFile(1080, 1812);
30-
makeFile(1080, 1920);//xxdpi x1(代表 基准中的一份既 1/320) :1080/320*1(以1080 分成320份在取一份)=3.375px;
31-
makeFile(1440, 2560);
32-
}
33-
34-
public static void makeFile(int w, int h) {
35-
36-
StringBuffer sb = new StringBuffer();
37-
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
38-
sb.append("<resources>");
39-
float cellw = w / dw;
40-
for (int i = 1; i <= dw; i++) {
41-
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
42-
change(cellw * i) + ""));
43-
}
44-
sb.append("</resources>");
45-
46-
StringBuffer sb2 = new StringBuffer();
47-
sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
48-
sb2.append("<resources>");
49-
float cellh = h / dh;
50-
for (int i = 1; i <=dh; i++) {
51-
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
52-
change(cellh * i) + ""));
53-
}
54-
sb2.append("</resources>");
55-
56-
String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
57-
File rootFile = new File(path);
58-
if (!rootFile.exists()) {
59-
rootFile.mkdirs();
60-
}
61-
File layxFile = new File(path + "lay_x.xml");
62-
File layyFile = new File(path + "lay_y.xml");
63-
try {
64-
PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
65-
pw.print(sb.toString());
66-
pw.close();
67-
pw = new PrintWriter(new FileOutputStream(layyFile));
68-
pw.print(sb2.toString());
69-
pw.close();
70-
} catch (FileNotFoundException e) {
71-
e.printStackTrace();
72-
}
73-
74-
}
75-
76-
//保留两位小数
77-
public static float change(float a) {
78-
return Math.round( a * 100 ) / 100.0F;
79-
}
1+
package 适配;
2+
import java.io.File;
3+
import java.io.FileNotFoundException;
4+
import java.io.FileOutputStream;
5+
import java.io.PrintWriter;
6+
7+
//values-1024x600... 适配
8+
public class MakeXml_AccurateSize {
9+
private final static String rootPath="/Users/fuzhipeng/Documents/layoutroot/values-{0}x{1}/";//Mac
10+
11+
// private final static String rootPath = "C:\\Users\\Administrator\\Desktop\\layoutroot\\values-{0}x{1}\\";
12+
13+
private final static float dw = 720f;//不当成像素 当成多少份
14+
private final static float dh = 1280f;
15+
16+
private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
17+
private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
18+
19+
public static void main(String[] args) {
20+
makeFile(320, 480);//mdpi
21+
makeFile(480,800);
22+
makeFile(480, 854);//hdpi
23+
makeFile(540, 960);
24+
makeFile(600, 1024);
25+
makeFile(720, 1184);
26+
makeFile(720, 1196);
27+
makeFile(720, 1280);//xdpi
28+
makeFile(768, 1024);
29+
makeFile(800, 1280);
30+
makeFile(1080, 1812);
31+
makeFile(1080, 1920);//xxdpi x1(代表 基准中的一份既 1/320) :1080/320*1(以1080 分成320份在取一份)=3.375px;
32+
makeFile(1440, 2560);
33+
}
34+
35+
public static void makeFile(int w, int h) {
36+
37+
StringBuffer sb = new StringBuffer();
38+
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
39+
sb.append("<resources>");
40+
float cellw = w / dw;
41+
for (int i = 1; i <= dw; i++) {
42+
sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
43+
change(cellw * i) + ""));
44+
}
45+
sb.append("</resources>");
46+
47+
StringBuffer sb2 = new StringBuffer();
48+
sb2.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
49+
sb2.append("<resources>");
50+
float cellh = h / dh;
51+
for (int i = 1; i <=dh; i++) {
52+
sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
53+
change(cellh * i) + ""));
54+
}
55+
sb2.append("</resources>");
56+
57+
String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
58+
File rootFile = new File(path);
59+
if (!rootFile.exists()) {
60+
rootFile.mkdirs();
61+
}
62+
File layxFile = new File(path + "lay_x.xml");
63+
File layyFile = new File(path + "lay_y.xml");
64+
try {
65+
PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
66+
pw.print(sb.toString());
67+
pw.close();
68+
pw = new PrintWriter(new FileOutputStream(layyFile));
69+
pw.print(sb2.toString());
70+
pw.close();
71+
} catch (FileNotFoundException e) {
72+
e.printStackTrace();
73+
}
74+
75+
}
76+
77+
//保留两位小数
78+
public static float change(float a) {
79+
return Math.round( a * 100 ) / 100.0F;
80+
}
8081
}

0 commit comments

Comments
 (0)