This article describes the Android programming to achieve the real width of the mobile phone screen method. Share to everyone for your reference, specific as follows:
WindowManager w = Activity.getwindowmanager ();
Display d = w.getdefaultdisplay ();
Displaymetrics metrics = new Displaymetrics ();
D.getmetrics (metrics);
Since sdk_int = 1;
Widthpixels = Metrics.widthpixels;
Heightpixels = Metrics.heightpixels;
try {
//used when > Sdk_int >= includes window decorations (statusbar bar/menu bar)
widthpixels = (I Nteger) Display.class.getMethod ("Getrawwidth"). Invoke (d);
Heightpixels = (Integer) Display.class.getMethod ("Getrawheight"). Invoke (d);
} catch (Exception ignored) {
}
try {
//used when Sdk_int >= includes window decorations (StatusBar b Ar/menu bar) point
realsize = new Point ();
Display.class.getMethod ("Getrealsize", Point.class). Invoke (d, realsize);
Widthpixels = realsize.x;
Heightpixels = realsize.y;
} catch (Exception ignored) {
}
Complement: Improved version (made up of some exceptions to the original unsupported version):
WindowManager w = Activity.getwindowmanager ();
Display d = w.getdefaultdisplay ();
Displaymetrics metrics = new Displaymetrics ();
D.getmetrics (metrics);
Since sdk_int = 1;
Widthpixels = Metrics.widthpixels;
Heightpixels = Metrics.heightpixels;
Includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= && build.version.s Dk_int <)
try {
widthpixels = (Integer) Display.class.getMethod ("Getrawwidth"). Invoke (d);
Heightpixels = (Integer) Display.class.getMethod ("Getrawheight"). Invoke (d);
} catch (Exception ignored) {
}
//Includes window decorations (statusbar bar/menu bar)
if ( Build.VERSION.SDK_INT >=
try {point
realsize = new Point ();
Display.class.getMethod ("Getrealsize", Point.class). Invoke (d, realsize);
Widthpixels = realsize.x;
Heightpixels = realsize.y;
} catch (Exception ignored) {
}
I hope this article will help you with the Android program.