Android-------Phone screen adaptation, file adaptation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.