Android gets screen size and density

Source: Internet
Author: User

from://http://blog.csdn.net/ithomer/article/details/6688883

Android to get the screen longer than the width, referring to the internet has a lot of code, but the results are inconsistent with the actual, such as my phone is i9000, the screen size is 480*800px, the results are 320*533

The results are very unreliable, so I wrote a few lines of code, pro-Test

Test parameters:

Test environment: i9000 (Samsung)

Physical screen: 480*800px

density:1.5

Test code:

[Java]View Plaincopyprint?
  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 results are as follows:

[Java]View Plaincopyprint?
  1. e/== myscreenactivity =================================== getdefaultdisplay ( 8509): screenWidth=320; screenheight=533
  2. e/== myscreenactivity =================================== displaymetrics ( 8509): xdpi=156.3077; ydpi= 157.51938
  3. e/== myscreenactivity =================================== displaymetrics ( 8509): density=1.0; densityDPI= the
  4. e/== myscreenactivity =================================== displaymetrics (111)(8509 ): screenWidth=320; screenheight=533
  5. e/== myscreenactivity =================================== displaymetrics ( 8509): xdpi=234.46153; ydpi= 236.27907
  6. e/== myscreenactivity =================================== displaymetrics ( 8509): density=1.5; densityDPI= -
  7. e/== myscreenactivity =================================== displaymetrics (222)(8509 ): screenWidthDip=  320; screenheightdip=533
  8. e/== myscreenactivity =================================== displaymetrics (222)(8509 ): screenWidth=480; screenheight=

Analysis results:

In the OnDraw () method

Methods 1 and 2, the results are consistent, are 320*533, obviously not the test machine i9000 screen size

Method 3, the result of the method 1 and 2, multiplied by density, the perfect 480*800,perfect!

Note: If the density is greater than 1, you need to set targetsdkversion between 4-9, for example
<USES-SDK android:minsdkversion= "3" android:targetsdkversion= "ten"/>

However, does this mean that method 3 must be generic?

The answer is no, because I also tested on the simulator, the HTC G14 physical machine, and the ViewSonic, Galaxy tablet, Method 3 magnified the actual screen value at density=1.5, for example: HTC G14

On the HTC G14, the actual screen size, directly through the Dm.widthpixels, dm.heightpixels to get the actual physical screen size (540,960)

The reason why the actual physical screen size cannot be obtained through a common approach may be that the Android system is open source and that different handset manufacturers do not have a uniform manufacturing standard to specify the phone screen.

Carefully analyze the code to find the problem in the code:

Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM)

Initialize a Displaymetrics object from this display ' s data.

DM = Getresources (). Getdisplaymetrics ()

Return The current display metrics that is in effect for this resource object. The returned object should be treated as read-only.

Test source

Android gets screen size and density

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.