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?
- Get screen density (Method 1)
- int screenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth (); //Screen width (pixels, for example: 480px)
- int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); //Screen height (pixels, for example: 800p)
- LOG.E (TAG + "Getdefaultdisplay", "screenwidth=" + screenwidth + "; screenheight= "+ screenheight);
- Get screen density (Method 2)
- Displaymetrics DM = new Displaymetrics ();
- DM = Getresources (). Getdisplaymetrics ();
- float density = dm.density; //Screen density (Pixel scale: 0.75/1.0/1.5/2.0)
- int densitydpi = dm.densitydpi; //Screen density (pixels per inch: 120/160/240/320)
- float xdpi = dm.xdpi;
- float ydpi = dm.ydpi;
- LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + "; Ydpi= "+ ydpi);
- LOG.E (TAG + "Displaymetrics", "density=" + density + "; Densitydpi= "+ densitydpi);
- ScreenWidth = Dm.widthpixels; //Screen width (pixels, for example: 480px)
- ScreenHeight = Dm.heightpixels; //Screen height (pixels, for example: 800px)
- LOG.E (TAG + "Displaymetrics (111)", "screenwidth=" + screenwidth + "; screenheight= "+ screenheight);
- Get screen density (Method 3)
- DM = new Displaymetrics ();
- Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
- density = dm.density; //Screen density (Pixel scale: 0.75/1.0/1.5/2.0)
- densitydpi = dm.densitydpi; //Screen density (pixels per inch: 120/160/240/320)
- xdpi = dm.xdpi;
- ydpi = dm.ydpi;
- LOG.E (TAG + "Displaymetrics", "xdpi=" + xdpi + "; Ydpi= "+ ydpi);
- LOG.E (TAG + "Displaymetrics", "density=" + density + "; Densitydpi= "+ densitydpi);
- int screenwidthdip = Dm.widthpixels; //Screen width (dip, for example: 320dip)
- int screenheightdip = Dm.heightpixels; //Screen width (dip, for example: 533dip)
- LOG.E (TAG + "displaymetrics (222)", "screenwidthdip=" + Screenwidthdip + "; screenheightdip= "+ Screenheightdip);
- ScreenWidth = (int) (Dm.widthpixels * density + 0.5f); //Screen width (px, for example: 480px)
- ScreenHeight = (int) (Dm.heightpixels * density + 0.5f); //Screen height (px, for example: 800px)
- LOG.E (TAG + "displaymetrics (222)", "screenwidth=" + screenwidth + "; screenheight= "+ screenheight);
The results are as follows:
[Java]View Plaincopyprint?
- e/== myscreenactivity =================================== getdefaultdisplay ( 8509): screenWidth=320; screenheight=533
- e/== myscreenactivity =================================== displaymetrics ( 8509): xdpi=156.3077; ydpi= 157.51938
- e/== myscreenactivity =================================== displaymetrics ( 8509): density=1.0; densityDPI= the
- e/== myscreenactivity =================================== displaymetrics (111)(8509 ): screenWidth=320; screenheight=533
- e/== myscreenactivity =================================== displaymetrics ( 8509): xdpi=234.46153; ydpi= 236.27907
- e/== myscreenactivity =================================== displaymetrics ( 8509): density=1.5; densityDPI= -
- e/== myscreenactivity =================================== displaymetrics (222)(8509 ): screenWidthDip= 320; screenheightdip=533
- 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