Android Combat Skills Ten: Get screen physical size, density and resolution

Source: Internet
Author: User

Everybody help!

Bloggers participate in 2014 blog star activities, we help vote! Punch here!


It is a very interesting thing to know the hardware through the program. I studied the physical size of the screen on WM6.5, but it has not been successful. Later, I wanted to make a breakthrough on Android, but I didn't get the exact size before today. Although many people think that there is no need to be so serious, because it seems that many circumstances do not use. But I'll take that as a very challenging thing, be sure to do it. Yes, it's so willful.


The display class under the Android.view package in the source code provides a number of ways for programmers to get display-related information, which allows us to start the tour of understanding the device screen.

First, the resolution needs to pay attention to the original often used GetHeight () and GetWidth () has been deprecated, it is recommended to use the GetSize () to replace.
This method is prototyped as follows:
    public void GetSize (point outsize) {        synchronized (this) {            updatedisplayinfolocked ();            Mdisplayinfo.getappmetrics (Mtempmetrics, mdisplayadjustments);            outsize.x = Mtempmetrics.widthpixels;            Outsize.y = Mtempmetrics.heightpixels;        }    }
parameter is a return parameter to return the point of resolution, this point is also relatively simple, we only need to focus on the X and y two members can be.
Use the following:
    private void Getdisplayinfomation () {point point        = new Point ();        Getwindowmanager (). Getdefaultdisplay (). GetSize (point);        LOG.D (TAG, "The screen size is" +point.tostring ());    }
The results are as follows:
d/mainactivity:the screen size was point (800, 1280)

The display also provides a Getrealsize method with the following prototype:
    public void Getrealsize (point outsize) {        synchronized (this) {            updatedisplayinfolocked ();            outsize.x = Mdisplayinfo.logicalwidth;            Outsize.y = mdisplayinfo.logicalheight;        }    }

There are differences between the implementations of the two methods, but in general the return values are the same. So where is the difference, do some experiments to verify it.
First, I'll set the acitvity to different theme, such as:
Android:theme= "@android: Style/theme.black.notitlebar.fullscreen" android:theme= "@android: style/ Theme.NoTitleBar.Fullscreen "
The results are still the same.

Next, turn my activity parent class into Actionbaractivity, as follows:
public class Mainactivity extends actionbaractivity
Expect the Actionbar to occupy some screens and dynamically set the picture size in the ListView item in the program. Under the coincidence of Chance,
The results verify that in this case, the results returned by the GetSize have changed.
The code is as follows:
    private void Getdisplayinfomation () {point point        = new Point ();        Getwindowmanager (). Getdefaultdisplay (). GetSize (point);        LOG.D (TAG, "The screen size is" +point.tostring ());        Getwindowmanager (). Getdefaultdisplay (). Getrealsize (point);        LOG.D (TAG, "the screen real size is" +point.tostring ());    }

Log as follows:
d/mainactivity:the screen size was point (1202) d/mainactivity:the screens real size is point (800, 1280)

If you are not able to reproduce easily and don't worry, be on the safe side, in order to get a relatively accurate message or use Getrealsize ().
Second, the screen size device physical screen size. Unlike a few years ago, the current phone screen is too big to hold. Standard early has reached the 5-inch screen era.
The so-called screen size refers to the length of the diagonal of the screen, in inches.
However, different screen sizes can be of the same resolution, and the difference between them is different from the density (density).
Let's introduce the concept of density, DPI, PPI, and finally how to find out the screen size based on the display information obtained. This is a problem that has been bothering me for a long time.
Three, screen density screen density and dpi this concept is closely linked, DPI is dots-per-inch, that is, the number of dots per inch. In other words, the greater the density, the more points are accommodated per inch.
There is a Displaymetrics class under the Android.util package to obtain density-related information.
The most important is the DENSITYDPI member, which has several common values as follows:
Density_low = 120density_medium =  Max//default value DENSITY_TV = 213      //tv Dedicated density_high = 240density_xhigh = 320density_ 400density_xxhigh = 480density_xxxhigh = 640

Examples are as follows:
    private void Getdensity () {        Displaymetrics displaymetrics = Getresources (). Getdisplaymetrics ();        LOG.D (TAG, "Density is" +displaymetrics.density+ "densitydpi is" +displaymetrics.densitydpi+ "Height:" + displaymetrics.heightpixels+            "width:" +displaymetrics.widthpixels);    }

Log as follows:
The screen, 2438, and the screen real size are point (2560, Density) are 2.0 densitydpi width:1600

With this information, can we calculate the screen size?
First, the diagonal length is measured in pixels.
The length of the diagonal is then calculated by dividing it by the density (densitydpi).
The code is as follows:
    private void Getscreensizeofdevice () {        displaymetrics dm = getresources (). Getdisplaymetrics ();        int width=dm.widthpixels;        int height=dm.heightpixels;        Double x = Math.pow (width,2);        Double y = Math.pow (height,2);        Double diagonal = math.sqrt (x+y);        int dens=dm.densitydpi;        Double screeninches = diagonal/(double) dens;        LOG.D (TAG, "the Screeninches" +screeninches);    

Log as follows:

01-13 16:35:03.026  16601-16601/com.linc.listviewanimation d/mainactivity:the screen size was point (1600, 2438) 01-13 16:35:03.026  16601-16601/com.linc.listviewanimation d/mainactivity:the screen real size was point (1600, 2560) 01-13 16:35:03.026  16601-16601/com.linc.listviewanimation d/mainactivity:density is 2.0 densityDpi is the height: 2438 width:1600 xdpi 338.666 ydpi 338.66601-13 16:35:03.026 16601-16601/com.linc.listviewanimation  D/ Mainactivity:the screeninches 9.112922229586951

As the log shows, the value obtained using Heightpixels is 2483 instead of the correct 2560. This results in the result that 9.11 is close to the real screen size. The following is calculated again with the correct height.
01-13 16:39:05.476  17249-17249/com.linc.listviewanimation d/mainactivity:the screen size was point (1600, 2560) 01-13 16:39:05.476  17249-17249/com.linc.listviewanimation d/mainactivity:the screen real size was point (1600, 2560) 01-13 16:39:05.476  17249-17249/com.linc.listviewanimation d/mainactivity:density is 2.0 densityDpi is the height: 2560 width:1600 xdpi 338.666 ydpi 338.66601-13 16:39:05.476 17249-17249/com.linc.listviewanimation  D/ Mainactivity:the screeninches 9.433981132056605
The result is 9.43 inches, and the real value is 8.91. If you change another device, the value is much worse. Indicates that the above calculation is incorrect.
So where's the mistake? DENSITYDPI is the number of dots per inch (Dots-per-inch) is the printer's common unit (and thus also known as the print resolution), not the number of pixels per inch. The concept of PPI is shown below.
Four, ppipixels per inch, this is the number of pixels I want (also known as the sample rate of the image). With this value, the physical size of the screen can be derived from the above formula.
Fortunately Displaymetrics has two members who are xdpi and ydpi, describing them as:
The exact physical pixels per inch of the screen in the X/y dimension.
The real physical PPI on the screen x/y axis.
yes! Got it!
To ensure the correct resolution, I use getrealsize to get the screen width and high pixels. So, after modification, the code is as follows:
    private void GetScreenSizeOfDevice2 () {point point        = new Point ();        Getwindowmanager (). Getdefaultdisplay (). Getrealsize (point);        Displaymetrics DM = getresources (). Getdisplaymetrics ();        Double x = Math.pow (point.x/dm.xdpi, 2);        Double y = Math.pow (point.y/dm.ydpi, 2);        Double screeninches = math.sqrt (x + y);        LOG.D (TAG, "screen inches:" + screeninches);    }
Log is as follows:
01-13 16:58:50.142  17249-17249/com.linc.listviewanimation d/mainactivity:screen inches:8.914015757534717
V. Dip note do not confuse with the above DPI, this dip is density independent Pixel, literal translation is a density-independent pixel.
The Dp/dip that we use in the layout file is it. DP is officially recommended because it calculates the corresponding pixel based on the density of your device.
The formula is: Pixel = dip*density

It is important to note that we do not set the width of the control in Java code to set the unit, and its own units are pixels. So if you dynamically modify the size of the control,
Our mission is to convert the pixels to DP.
The instance code is as follows:
    pixel = dip*density;    private int Convertdptopixel (int dp) {        Displaymetrics displaymetrics = Mcontext.getresources (). Getdisplaymetrics ( );        return (int) (dp*displaymetrics.density);    }    private int convertpixeltodp (int pixel) {        displaymetrics displaymetrics = mcontext.getresources (). Getdisplaymetrics ();        return (int) (pixel/displaymetrics.density);    

Reference:

Http://stackoverflow.com/questions/19155559/how-to-get-android-device-screen-size

Android Combat Skills Ten: Get screen physical size, density and resolution

Related Article

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.