Top 10 practical Android skills: Obtain the physical size, density, and resolution of the screen.

Source: Internet
Author: User

Top 10 practical Android skills: Obtain the physical size, density, and resolution of the screen.

Help me!

The blogger participated in the 2014 blog star event. You can vote for it! Click here!


It is very interesting to know the hardware situation through a program. I studied how to obtain the physical size of the screen on WM6.5 a long time ago, but it has never been successful. Later, I wanted to make some breakthroughs in Android, but the size obtained before today was not accurate. Although many people think it is not necessary to be so real, it seems that it is not used in many cases. However, this is a very challenging task and must be done. Yes, it's so capricious.


In the source code, the Display class under the android. view package provides many methods for programmers to obtain information about Display. With this class, let's start a tour of device screens.

1. the commonly used getHeight () and getWidth () are not recommended for resolution. We recommend that you use getSize () instead.
The prototype of this method is as follows:
    public void getSize(Point outSize) {        synchronized (this) {            updateDisplayInfoLocked();            mDisplayInfo.getAppMetrics(mTempMetrics, mDisplayAdjustments);            outSize.x = mTempMetrics.widthPixels;            outSize.y = mTempMetrics.heightPixels;        }    }
The parameter is a return parameter used to return the resolution Point. This Point is also relatively simple. We only need to pay attention to the two Members x and y.
The usage is as follows:
    private void getDisplayInfomation() {        Point point = new Point();        getWindowManager().getDefaultDisplay().getSize(point);        Log.d(TAG,"the screen size is "+point.toString());    }
The result is as follows:
D/MainActivity﹕ the screen size is Point(800, 1280)

In addition, Display provides a getRealSize method. The prototype is as follows:
    public void getRealSize(Point outSize) {        synchronized (this) {            updateDisplayInfoLocked();            outSize.x = mDisplayInfo.logicalWidth;            outSize.y = mDisplayInfo.logicalHeight;        }    }

There are differences between the implementation of the two methods, but the return values of the two methods are usually the same. So where is the difference? Here are some experiments to verify it.
First, I set different theme for acitloud, for example:
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
The results are still the same.

Next, change the parent class of my Activity to ActionBarActivity, as shown below:
Public class MainActivity extends ActionBarActivity
It is expected that the ActionBar will occupy some screens and dynamically set the image size in the Listview Item in the program. Coincidentally,
The results verify that the results returned by getSize have changed in this case.
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());    }

The Log is as follows:
D/MainActivity﹕ the screen size is Point(800, 1202)D/MainActivity﹕ the screen real size is Point(800, 1280)

If you cannot reproduce it easily, you don't need to worry about it. For the sake of insurance, use getRealSize () to obtain the correct information.
2. screen size the physical screen size of the device. Different from a few years ago, the current mobile phone screen is too big to be held. The standard has already reached the 5-inch screen era.
The screen size refers to the diagonal length of the screen, measured in inches.
However, different screen sizes can adopt the same resolution, and the difference between them is different from the density.
Next we will first introduce the concept of density, DPI and PPI, and finally explain how to find the screen size based on the displayed information. This is a problem that has plagued me for a long time.
3. Screen density the screen density is closely related to the DPI concept. DPI is a combination of dots-per-inch, that is, the number of points per inch. That is to say, the larger the density, the more points each inch can hold.
The android. util package has a DisplayMetrics class to obtain density-related information.
The most important is the member densityDpi, which has the following common values:
DENSITY_LOW = 120DENSITY_MEDIUM = 160 // default value DENSITY_ TV = 213 // TV dedicated DENSITY_HIGH = 240DENSITY_XHIGH = 320DENSITY_400 = 400DENSITY_XXHIGH = 480DENSITY_XXXHIGH = 640

Example:
    private void getDensity() {        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();        Log.d(TAG,"Density is "+displayMetrics.density+" densityDpi is "+displayMetrics.densityDpi+" height: "+displayMetrics.heightPixels+            " width: "+displayMetrics.widthPixels);    }

The Log is as follows:
the screen size is Point(1600, 2438)the screen real size is Point(1600, 2560)Density is 2.0 densityDpi is 320 height: 2438 width: 1600

With this information, can we calculate the screen size?
First, obtain the diagonal line length in pixels.
Divide it by density (densityDpi) to get the diagonal line length.
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);    }

The Log is as follows:

01-13 16:35:03.026  16601-16601/com.linc.listviewanimation D/MainActivity﹕ the screen size is Point(1600, 2438)01-13 16:35:03.026  16601-16601/com.linc.listviewanimation D/MainActivity﹕ the screen real size is Point(1600, 2560)01-13 16:35:03.026  16601-16601/com.linc.listviewanimation D/MainActivity﹕ Density is 2.0 densityDpi is 320 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 shown in Log, the value obtained by using heightPixels is 2483 instead of 2560, so that the result 9.11 is very similar to the real screen size. Calculate the value again with the correct height.
01-13 16:39:05.476  17249-17249/com.linc.listviewanimation D/MainActivity﹕ the screen size is Point(1600, 2560)01-13 16:39:05.476  17249-17249/com.linc.listviewanimation D/MainActivity﹕ the screen real size is Point(1600, 2560)01-13 16:39:05.476  17249-17249/com.linc.listviewanimation D/MainActivity﹕ Density is 2.0 densityDpi is 320 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 inch, and the actual value is. If you change another device, the value difference is more. The above calculation is incorrect.
So where is the error? DensityDpi is the number of points per inch (dots-per-inch) is a common unit of printer (and thus known as the print resolution), rather than the number of pixels per inch. The concept of PPI is introduced below.
4. PPIPixels per inch. This is the number of pixels per inch (also known as the image sampling rate ). With this value, you can calculate the physical size of the screen according to the above formula.
Fortunately, DisplayMetrics has two members: xdpi and ydpi, which are described as follows:
//The exact physical pixels per inch of the screen in the X/Y dimension.
The real physical PPI on the X/y axis of the screen.
Yes! Got it!
To ensure correct resolution, I still use getRealSize to get the screen width and height pixels. Therefore, 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
5. Do not confuse DIP with the above DPI. This DIP is Density Independent Pixel, which is a Density-Independent Pixel.
The dp/dip we use in the layout file is it. We recommend that you use dp because it calculates the corresponding pixels Based on the density of your device.
Formula: pixel = dip * density

It should be noted that we cannot set the Unit for setting the width and height of the control in Java code, and its built-in unit is pixel. Therefore, if the widget is dynamically modified,
Our task is to convert 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);    } 

Refer:

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

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.