Android programming to achieve the real width of the phone screen _android method

Source: Internet
Author: User

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.

Related Article

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.