Android to get the screen longer than wide, reference to the internet there are a lot of code, but the results are not practical, such as my phone is i9000, screen size is 480*800px, the result is 320*533
The result is very unreliable, so I wrote a few lines of code, a close test
Test parameters:
Test environment: i9000 (Samsung)
Physical screen: 480*800px
density:1.5
Test Code :
Copy Code code as follows:
Get screen density (Method 1)
int screenwidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth (); Screen width (pixels, such as: 480px)
int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); High screen (pixel, such as: 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, such as: 480px)
ScreenHeight = Dm.heightpixels; High screen (pixel, such as: 800px)
LOG.E (TAG + "Displaymetrics", "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, such as: 320dip)
int screenheightdip = Dm.heightpixels; Screen width (dip, such as: 533dip)
LOG.E (TAG + "displaymetrics (222)", "screenwidthdip=" + Screenwidthdip + "; screenheightdip= "+ Screenheightdip);
ScreenWidth = (int) (Dm.widthpixels * density + 0.5f); Screen width (px, such as: 480px)
ScreenHeight = (int) (Dm.heightpixels * density + 0.5f); High screen (PX, such as: 800px)
LOG.E (TAG + "displaymetrics (222)", "screenwidth=" + ScreenWidth + "; screenheight= "+ screenheight);
The results are as follows:
Copy Code code as follows:
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=160
e/== myscreenactivity =================================== displaymetrics (8509): screenwidth=320; screenheight=533
e/== myscreenactivity =================================== displaymetrics (8509): xdpi=234.46153; ydpi=236.27907
e/== myscreenactivity =================================== displaymetrics (8509): density=1.5; densitydpi=240
e/== myscreenactivity =================================== displaymetrics (222) (8509): screenWidthDip=320; screenheightdip=533
e/== myscreenactivity =================================== displaymetrics (222) (8509): screenWidth=480; screenheight=800
Analysis Results:
In the OnDraw () method
Methods 1 and 2, the results are consistent, are 320*533, is obviously not the test machine i9000 screen size
Method 3, will be the results of methods 1 and 2, multiplied by density, after the perfect 480*800,perfect!
Note: Density is greater than 1, you need to set targetsdkversion between 4-9, for example
<USES-SDK android:minsdkversion= "3" android:targetsdkversion= "ten"/>
But does that mean that method 3 must be generic?
The answer is no, because I've tested the simulator, the HTC G14 Physics machine, and the ViewSonic, the Galaxy Tablet, and Method 3 magnifies the actual screen value when 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 it is impossible to get a real physical screen size in a generic way may be that the Android system is open source, and different handset manufacturers do not have uniform manufacturing standards to provide a mobile screen.
analyze the code carefully and find out the problem is in the code:
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM)
Initialize a Displaymetrics object from this display ' s data.
DM = Getresources (). Getdisplaymetrics ()
Return the "Current display metrics" are in effect to this resource object. The returned object should be treated as read-only.