public class Main {
Defines the file local storage path, which can be changed as required
Private final static String RootPath = "D:\\workfiles\\layoutroot\\values-{0}x{1}";
/** horizontal axis is divided into 320 parts */
Private final static float DW = 320f;
/** longitudinal axis direction is divided into 480 parts */
Private final static float DH = 480f;
/** width of the template */
Private final static String wtemplate = "<dimen name=\" x{0}\ ">{1}px</dimen>\n";
/** Height Template */
Private final static String htemplate = "<dimen name=\" y{0}\ ">{1}px</dimen>\n";
public static void Main (string[] args) {
Three-resolution phone screen
Makestring (320, 480);
Makestring (720, 1280);
Makestring (1080, 1920);
}
/**
* Generate Dimen file with corresponding resolution
*
* @param width
* Number of pixels in x direction of phone screen
* @param height
* Number of pixels in the Y-direction of the phone screen
*/
private static void makestring (int width, int height) {
StringBuffer sbwidth = new StringBuffer ();
Sbwidth.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n");
Sbwidth.append ("<resources>");
float CELLW = WIDTH/DW;
for (int i = 0; i < DW; i++) {
Sbwidth.append (Wtemplate.replace ("{0}", I + ""). Replace ("{1}",
Change (CELLW * i) + "");
}
Sbwidth.append ("{0}", "Wtemplate.replace"). Replace ("{1}",
Width + ""));
Sbwidth.append ("</resources>");
StringBuffer sbheight = new StringBuffer ();
Sbheight.append ("<?xml version=\" 1.0\ "encoding=\" utf-8\ "? >\n");
Sbheight.append ("<resources>");
float CELLH = HEIGHT/DH;
for (int i = 0; i < dh; i++) {
Sbheight.append (Htemplate.replace ("{0}", I + ""). Replace ("{1}",
Change (CELLH * i) + "");
}
Sbheight.append (Htemplate.replace ("{0}", "480"). Replace ("{1}",
Height + ""));
Sbheight.append ("</resources>");
String Path = Rootpath.replace ("{0}", Height + ""). Replace ("{1}",
Width + "");
File RootFile = new file (path);
if (!rootfile.exists ()) {
Rootfile.mkdirs ();
}
File Layxfile = new file (path + "\\lay_x.xml");
File Layyfile = new file (path + "\\lay_y.xml");
try {
PrintWriter pw = new PrintWriter (new FileOutputStream (Layxfile));
Pw.print (Sbwidth.tostring ());
Pw.close ();
PW = new PrintWriter (new FileOutputStream (Layyfile));
Pw.print (Sbheight.tostring ());
Pw.close ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* Keep floating-point numbers two decimal places after the decimal point
*/
private static float change (float a) {
int temp = (int) (A * 100);
return temp/100f;
}
}
Copy the resulting values file to the project's res,
Use cases:
Android:layout_width= "@dimen/x160"
Android-------Phone screen adaptation, file adaptation