DPI, dip, resolution, screen size, px, density relationships, and conversions

Source: Internet
Author: User

first, the basic concept
    • Dip:density independent pixels, device independent pixels.

    • DP: Just Dip

    • PX: pixels

    • dpi : dots per inch, which is directly the number of pixels in an inch. A common value of 120,160,240. I am generally referred to as pixel density, short density

    • density : Direct translation of the words seems to be called density. Common values are 1.5, 1.0. Proportional to the standard dpi (160PX/INC)

    • Resolution: The number of pixel points in 2 directions, common value 480X800, 320X480

    • Screen Size: The length of the diagonal of the screen. Computer TV is the same.

    • The problem with the screen scale. Because only the diagonal length is determined, 2-side lengths are not necessarily. So there are 4:3, 16:9 of this, so you can calculate the length of the screen side.

second, the application

Inside Android, get a window of metrics, with so few values inside

metrics.density; metrics.densitydpi;

DENSITYDPI: That's what we always say about DPI.

Density: The value that is actually obtained after DPI/(160 pixels/inch). Isn't it a little strange, because I brought the unit ... This involves a more important thing in the back, and then again.

From the above can be seen, DPI itself is pixels/inches, so density is actually no unit, he is a proportional value.

and DPI units are pixels/inches, compared to the physical above the density definition, density is not all units measured value, so I prefer to call the dpi pixel density, called density, density or called density.

Iii. conversion between units 1. Calculate DPI  

For example a machine, screen 4 inch, resolution 480x800, his DPI can count it.
Because do not know the side length, certainly cannot separate calculates, 4 is the diagonal length, that directly uses the Pythagorean theorem to calculate the diagonal pixel, divides 4, calculates is probably dpi = 233 pixels/inch.
Then density is (233 px/inch)/(Px/inch) = 1.46 or so

Incidentally, Android default only 3 Dpi,low, medium and High, corresponding to 120, 160, 240, if there is no special settings, all DPI will be counted as these 3, specifically refer to this post
Http://android.tgbus.com/Android/tutorial/201103/347176.shtml
The default is 160.

2. Calculate DP and PX

When we write the layout, we must still know how many px the 1 DP actually has.

The conversion formula is as follows: DP = (dpi/(160 megapixels/inch)) px = density px

Note that these are all with units. PX is unit, DP is unit, density no unit.

For convenience, assuming DPI is 240 pixels per inch, then density is 1.5

So that's dp=1.5px, note that this is a unit, which is device-independent pixels = density pixels

Then the conversion to the numerical calculation, it should be the following formula

PX = density * DP

That is
Pixel value = density * device-independent pixel value, note that there is a value word here.

3. Why Standard DPI =

(1) Android Design [1] in the mainstream device DPI into four grades, three dpi, the DPI, the DPI, the

In actual development, we often need to convert these dimensions to each other (such as the first design at a certain resolution, and then zoom to other resizing after the output), generally according to the ratio of DPI is 2:1.5:1:0.75 to the elements in the interface to the dimension definition.

That is, if the size of the DP is 4 common multiple, XHDPI multiplied by the 2,hdpi under the 1.5,ldpi multiplied by 0.75 can satisfy all the dimensions are integer pixel.

But assuming a standard of up to 3 dpi, that requires DP to be common multiple, XHDPI multiplied by 1.333,mdpi under 0.666, ldpi divided by 2

The ldpi and XHDPI are more complex, so select the DPI

(2) This is explained in Google's official documentation, as the first Android device (HTC's T-mobile G1) is 160dpi.

Iv. Example Analysis 1. Screen size

is the phone screen size we usually speak, is the diagonal length of the screen, generally speaking the size of units are inches.
For example, IPhone5S's screen size is 4 inches. Samsung Note3 is 5.7 inches.

  

Figure 1

2. Pixels (pixel)

  Imagine zooming in and out of the screen, right! The small dots or small squares that you see are pixels.

Figure 2

3. Resolution (Resolution)

Refers to the number of pixels in the vertical and horizontal directions on the screen.
For example iphone5s resolution is 1136*640;samsung Note3 resolution is 1920*1080;

Figure 3

4.dpi

is the dot per inch abbreviation, is every inch of the number of pixels, also known as screen density. The larger the value, the clearer the screen will be.
The DPI of iphone5s is 326; the dpi of Samsung Note3 is 386

Figure 4

5.dip

  It is the abbreviation of density independent pixel, which refers to the pixels in the abstract sense. is related to the screen density of the device.

It is a unit in Android, dip and DP are the same.

Google's official note is this:
Density-independent Pixel (DP)
A Virtual pixel unit, should use when defining UI layout, to express layout dimensions or position in a density-in Dependent.
The density-independent pixel is equivalent to one physical pixel on a (DPI screen), which is the baseline density assum Ed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the DP units, as necessary, based on the actual density of the Screen on use. The conversion of DP units to screens pixels is SIMPLE:PX = DP * (dpi/160). For example, on a-DPI screen, 1 DP equals 1.5 physical pixels. Should always use DP units when defining your application ' s UI, to ensure proper display of your UI on screens with Di Fferent densities.

That means on the 160dpi screen, 1dip=1px.
It is related to screen density, if the screen density is large, 1dip represents more px, such as on the 320dpi screen, 1dip=2px.

Why should we use dip in the layout, not with PX?

Because there are many different screen densities in this world, what is the screen density? is the DPI, which is the number of pixels in the unit length.

Imagine, if the size of these phones, the screen density is very different, it is not said that a mobile phone in the horizontal direction of the pixel is very small, the other mobile phone in the horizontal direction of a lot of pixels? When we draw the same number of PIX, it shows

The length of the show will not be the same?

For example, the figure below two mobile phones, while setting the 2px length of the button, in the screen density of the mobile phone will be shown relatively small.

While setting the 2dip length button, the size shown on two phones is the same.

Figure 5

So if you use the PX as the unit in the app layout, then your app runs on every device and there's a weird phenomenon.

To look at the effect on the emulator, I defined two buttons, respectively, using PX and dip units.

This is what the layout file says.

<button android:layout_width= "100px" android:layout_height= "100px" android:text= "@string/str_button1"/> &L T Button android:layout_width= "100dip" android:layout_height= "100dip" android:text= "@string/str_button1"/>

The interface shown is this:

  

Figure 6

Getresources (). Getdisplaymetrics (). densitydpi is the screen density.
Getresources (). Getdisplaymetrics (). Density can also be understood as how many px the 1dip corresponds to.
The DPI above is 240,1dip=1.5px
You see, the 100dip button is 1.5 times times the length of 100pxButton.


DPI, dip, resolution, screen size, px, density relationships, and conversions

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.