Android Get screen resolution

Source: Internet
Author: User

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

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.