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