Original: Android Get screen resolution
There are three ways to get a screen size as follows:
//acquired via WindowManager
displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
//Acquired through Resources
displaymetrics dm2 = Getresources (). Getdisplaymetrics ();
//Get the default resolution of the screen
Display display = Getwindowmanager (). Getdefaultdisplay ();
System.out.println ("Width-display:" + display.getwidth ());
System.out.println ("Heigth-display:" + display.getheight ());
In the Android 4.2 system, the correct screen size value is obtained using method one, and the values obtained by using methods two and three are all zero. The reason is ominous. The Display.getwidth () and Display.getheight () in method three have been deprecated.
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); //display = Getwindowmanager (). Getdefaultdisplay ();d isplay.getmetrics (DM) (Assigning screen size information to displaymetrics DM);
width = dm.widthpixels;
Height = dm.heightpixels;
xdpi = dm.xdpi;
ydpi = dm.ydpi;
density = dm.densitydpi;
fdensity = dm.density;
Query log to get the values as follows:
D/lichking (22643):-----------widthpixels---540
D/lichking (22643):-----------heightpixels---960
D/lichking (22643):-----------xdpi---244.92857
D/lichking (22643):-----------ydpi---246.30302
D/lichking (22643):-----------Density---240
D/lichking (22643):-----------fdensity---1.5
Also, we usually mean that the size of the int on the screen is a pixel unit, for example, I use Gethitrect () to get the value of the control's rectangle coordinates even if it matches the pixel size.
The relationship between Displaymetrics and display.
Display refers to the object that displays the area, which may be a real physical screen, or only the display area of the application, such as in non-full-screen activity, because the system has a status bar, so the display area is smaller than the physical screen. The Displaymetrics encapsulates the various attribute values for the display area . View source discovery, in displaymetrics the comments for each attribute value are described as true physical dimensions. It is also found that the function of Display.getmetrics (DM) is basically applied when the real screen size is obtained. Remember that is good.
In addition, XDPI does not refer to the size of the screen width in DP units. The screen width in DP is calculated using the formula: Dpwidth = widthpixels/fdensity = 540/1.5 =
Android Get screen resolution