Android gets screen size and density

Source: Internet
Author: User

1. Screen size

That is, the actual size of the display screen, measured according to the diagonal of the screen.

For simplicity, Android puts all screen sizes into four sizes: small, normal, large, oversized (corresponding to: small, normal, large, and extra large).

The application can provide different custom screen layouts for each of these four sizes-the platform will be rendered according to the actual size of the screen, which is transparent to the program side.

2. Screen aspect ratio aspect ratio

The aspect ratio is the proportional relationship between the physical width of the screen and the physical height. An application can provide a screen layout resource for a specified aspect ratio by using a qualified resource.

3. Screen resolution resolution

The sum of the physical pixels displayed on the screen. It is important to note that although the resolution is usually expressed as a wide x-height, the resolution does not imply a specific aspect ratio of the screen.

In Andorid systems, the application does not directly use resolution.

4. density density

Depending on the pixel resolution, the number of pixels displayed in the screen specifies the physical wide-height range.

In the same wide-height area, low-density displays can display fewer pixels, while high-density displays can display more pixels.

Screen density is very important, as other conditions are constant, a total width and height of the fixed UI components (such as a button) on the low-density display is very large, and on the high-density display is very small.

For simplicity, Android puts all the screen resolutions into four sizes: small, normal, large, oversized (corresponding to: small, normal, large, and extra large).

Applications can provide different resources for each of these four sizes-the platform will transparently scale the resources to match the specified screen resolution.


density-independent pixels (DIP)
Refers to an abstract pixel, which the program uses to define the interface elements. It is a unit that is independent of the actual density and helps programmers build a layout scheme (width, height, and position of interface elements).
A density-independent pixel, on a logical size, that is consistent with a pixel on a screen with a pixel density of 160DPI, which is the default display device assumed by the Android platform. At run time, the platform takes the density of the target screen as a benchmark and "transparently" handles all required dip scaling operations. To convert density-independent pixels to screen pixels, you can use a simple formula: pixels = dips * (density/160). For example, on a screen with a dpi of 240, 1 dips equals 1.5 physical pixels. We strongly recommend that you use a dip to define the layout of your application's interface, as this will ensure that your UI will display properly on screens of all resolutions.
 In an activity's OnCreate method, write the following code:
Displaymetrics metric = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (Metric);
int width = metric.widthpixels; Screen width (pixels)
int height = metric.heightpixels; Screen height (pixels)
float density = metric.density; Screen Density (0.75/1.0/1.5)
int densitydpi = metric.densitydpi; Screen density dpi (120/160/240)
     However, it is important to note that on a low-density small-screen phone, only the above code is not able to get the correct size. For example, a 240x320 pixel low-density mobile phone, if you run the above code, the screen size obtained is 320x427. As a result, the study found that without multi-resolution support, the Android system would convert the 240x320 low-density (120) size to a medium-density (160) dimension, which would greatly affect the coding of the program. Therefore, in the project's Androidmanifest.xml file, add the Supports-screens node, the specific content is as follows:
         <supports-screens
            android: Smallscreens= "true"
            android: Normalscreens= "true"
            android: Largescreens= "true"
            android: Resizeable= "true"
            android: Anydensity= "true"/>
In this case, the current Android program supports a variety of resolutions, so you can get the correct physical size.   
  1. //Get screen density (Method 1)
  2. int screenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth (); //Screen width (pixels, for example: 480px)
  3. int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); //Screen height (pixels, for example: 800p)
  4. LOG.E (TAG + "Getdefaultdisplay", "screenwidth=" + screenwidth + "; screenheight= " + screenheight);
  5. //Get screen density (method 2)
  6. Displaymetrics DM = new displaymetrics ();
  7. DM = Getresources (). Getdisplaymetrics ();
  8. float density = dm.density; //Screen density (Pixel scale: 0.75/1.0/1.5/2.0)
  9. int densitydpi = dm.densitydpi; //Screen density (pixels per inch: 120/160/240/320)
  10. float xdpi = dm.xdpi;
  11. float ydpi = dm.ydpi;
  12. LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + "; ydpi= " + ydpi);
  13. LOG.E (TAG + "Displaymetrics", "density=" + density + "; densitydpi= " + densitydpi);
  14. ScreenWidth = Dm.widthpixels; //Screen width (pixels, for example: 480px)
  15. ScreenHeight = Dm.heightpixels; //Screen height (pixels, for example: 800px)
  16. LOG.E (TAG + "Displaymetrics (111)", "screenwidth=" + screenwidth + "; screenheight= " + screenheight);
  17. //Get screen density (method 3)
  18. DM = new displaymetrics ();
  19. Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
  20. density = dm.density; //Screen density (Pixel scale: 0.75/1.0/1.5/2.0)
  21. densitydpi = dm.densitydpi; //Screen density (pixels per inch: 120/160/240/320)
  22. xdpi = dm.xdpi;
  23. ydpi = dm.ydpi;
  24. LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + "; ydpi= " + ydpi);
  25. LOG.E (TAG + "Displaymetrics", "density=" + density + "; densitydpi= " + densitydpi);
  26. int Screenwidthdip = dm.widthpixels; //Screen width (dip, for example: 320dip)
  27. int Screenheightdip = dm.heightpixels; //Screen width (dip, for example: 533dip)
  28. LOG.E (TAG + "displaymetrics (222)", "screenwidthdip=" + Screenwidthdip + "; screenheightdip= " + Screenheightdip);
  29. ScreenWidth = (int) (dm.widthpixels * density + 0. 5f); //Screen width (px, for example: 480px)
  30. ScreenHeight = (int) (dm.heightpixels * density + 0. 5f); //Screen height (px, for example: 800px)
  31. LOG.E (TAG + "displaymetrics (222)", "screenwidth=" + screenwidth + "; screenheight= " + screenheight);

  the weight of the heaviest:The density value indicates how many display points per inch, with a resolution of two different concepts:

Android mainly has the following screens:

QVGA and WQVGA screen density=120;

HVGA screen density=160;

WVGA screen density=240;

The following is an example of 480dip*800dip's WVGA (density=240), which lists the screen resolution information for different density in detail:

Screen actual resolution is 240px*400px when density=120 (two points corresponds to a resolution)
Status bar and title bar height 19px or 25dip
Screen width 400px or 800dip, working area height 211px or 480dip
Screen width 240px or 480dip when vertical screen, working area height 381px or 775dip

DENSITY=160 screen Actual resolution is 320px*533px (3 points corresponds to two resolution)
Status bar and title bar tall 25px or 25dip
Screen width 533px or 800dip, working area height 295px or 480dip
Screen width 320px or 480dip when vertical screen, working area height 508px or 775dip

density=240 screen Actual resolution is 480px*800px (one point for one resolution)
Status bar and title bar tall 38px or 25dip
Screen width 800px or 800dip, working area height 442px or 480dip
Screen width 480px or 480dip when vertical screen, working area height 762px or 775dip

APK resource Bundle, when the screen density=240 use the hdpi tag resource
Resources that use MDPI tags when the screen is density=160
When the screen density=120, use the ldpi tag resource.
Resources that are not tagged are shared in a variety of resolution scenarios.
Recommendation: Use the unit dip as much as possible in the layout and use less px.

device independent pixels (unit independent pixels). Different devices have different display effects, this is related to the device hardware, generally we support WVGA, HVGA and QVGA recommend this, do not rely on pixels.


Android gets screen size and density

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.