Resolution
It should be noted that getHeight() and getWidth() are not recommended and getSize() is recommended to replace them.The prototype of this method is as follows:
A parameter is a return parameter that returns a Point of resolution. This Point is also relatively simple. We just need to focus on the two members x and y.
The usage is as follows:
The results are as follows:
In addition, Display provides a getRealSize method, which is prototyped as follows:
The implementation of the two methods is different, but the return values of the two methods are the same in general. So what is the difference? Here are some experiments to verify it.
First, I set Acitvity to different theme s, such as:
The results are the same.
Next, turn my ActionBarActivity parent into ActionBarActivity, as follows:
public class MainActivity extends ActionBarActivity
It is expected that ActionBar will occupy some screens and dynamically set the image size in the Item of Listview in the program. By chance,
The results verify that in this case, getSize returns a different result.
The code is as follows:
Log is as follows:
If you can't easily reproduce it and don't rush it, be sure to use getRealSize() in order to get the relatively correct information.
II. Screen Size
The physical screen size of the device. Unlike a few years ago, the mobile screen is now too big to hold in one hand. Standard matching has already reached the age of 5 inch screen.The so-called screen size refers to the length of the screen diagonal line in inches.
However, different screen sizes can use the same resolution, and the difference between them is different from density.
Next, we introduce the concept of density, DPI and PPI. Finally, we explain how to calculate the screen size according to the obtained Display information. This is a problem that has troubled me for a long time.
3. Screen Density
Screen density is closely related to the concept of DPI. DPI is dots-per-inch, the number of dots per inch. That is to say, the greater the density, the more points per inch of content.There is a DisplayMetrics class under the android.util package to get density-related information.
Most importantly, the member densityDpi has the following common values:
Examples are as follows:
Log is as follows:
With this information, can we calculate the screen size?
Firstly, the diagonal length in pixels is obtained.
Then divide it by density (Dpi) to get the length of the diagonal line.
The code is as follows:
Log is as follows:
As Log can see, using heightPixels gives a value of 2483 instead of the correct 2560. This brings the result of 9.11 closer to the real screen size. Let's do it again with the right height.
The result is 9.43 inches, and the real value is 8.91. If you change another device, the difference is more. It shows that the above calculation is wrong.
So what's wrong? densityDpi is the dots-per-inch unit commonly used by printers (hence also known as print resolution), rather than the number of pixels per inch. The concept of PPI is introduced below.
Four, PPI
Pixels per inch, that's the number of pixels per inch I want (also known as image sampling rate). With this value, the physical size of the screen can be derived from the above formula.Fortunately, DisplayMetrics has two members, xdpi and ydpi, which are described as:
Real physical PPI on the X/Y axis of the screen.
Yes!Got it!
To ensure the correct resolution, I still use getRealSize to get the screen width and high pixels. So, after modification, the code is as follows:
Log is as follows:
Five, DIP
Be careful not to be confused with the above DPI, which is Density Independent Pixel, literally translated into density-independent pixels.The dp/dip we use in the layout file is it. dp is officially recommended because it calculates the corresponding pixels according to the density of your device.
The formula is: pixel = dip*density
It's important to note that we can't set the width and height of the control in Java code, but the unit that comes with it is the pixel. So if you dynamically change the size of the control,
Our task is to convert pixels to dp.
The example code is as follows:
Reference resources:
http://stackoverflow.com/questions/19155559/how-to-get-android-device-screen-size