Many people know that using the WindowManager.getDefaultDisplay.getDisplayMetrics (Displaymetrics met) method to get the screen width, but this is not necessarily accurate, because sometimes, There will be a small window (the existence of Virtual display).
In this case, we need to find out the largest size of all display, as the actual size of the device (there may be more than the real size of the display, not specifically confirmed, this method does not apply)
The code is as follows:
Private StaticPoint getphysicalscreensize (context context) {point P=NewPoint (); Displaymanager DM=(Displaymanager) Context.getsystemservice (Context.display_service); Display[] Displays=dm.getdisplays (); intMaxWidth =-1, maxheight = 1; Displaymetrics met=NewDisplaymetrics (); for(Display d:displays) {d.getmetrics (MET); MaxWidth= met.widthpixels > MaxWidth?Met.widthPixels:maxWidth; MaxHeight= met.heightpixels > MaxHeight?Met.heightPixels:maxHeight; } p.x=MaxWidth; P.Y=MaxHeight; returnp; }
[Android] How to get the true size of the device