About dip, dpi, and resolution, dipdpi resolution

Source: Internet
Author: User

About dip, dpi, and resolution, dipdpi resolution

Resolution, which is very troublesome on the android platform, and causes many new users to crash. Today, I want to talk to the resolution guy.

First, what is resolution? We usually say 480*800, 1280*720, and so on, is the resolution of the mobile phone screen. Resolution represents the total number of pixels on the screen, that is, the concept of px in ps or other image editing software. When the artist is helping us build a high-definition prototype image, it usually depends on the resolution. Here, we would like to remind new users that it is best to select 1280*720 pixels for the HD Prototype Image of the android version, so that it will be easier for the artist to cut the image later. Currently, the resolution of the Project is 480*800. Therefore, many small icons need to be redone in the future for high-resolution adaptation. Otherwise, the display effect is blurred, this increases the workload of the UI Designer, which is a lesson!

However, when writing an interface using xml, our developers do not directly use px as the unit, because on the interface with the same display effect under different resolutions, the actual pixels of the control are not the same. Therefore, we usually use dip (dp) as the length unit when setting the control size, what kind of unit is dip?

Dip is the abbreviation of Density independent pixel, that is, the meaning of independent pixel Density. dip is an abstract unit based on screen Density and has nothing to do with the device. It is used to describe the size and position unrelated to the Density. It is relative to a DPI screen, so 1dip corresponds to 1px on the 160dpi screen.

Note that dip and dpi are two very different concepts.

Dip refers to an independent pixel representation, and its calculation is based on dpi, that is, we only know the dpi, in order to accurately calculate the required dip based on different dpi.

Dpi is a unit of density, short for dot per inch, that is, the number of pixels per inch. For example, if we have a mobile phone with a resolution of 480*800 and the physical size of the screen is 3 inch * 5 inch, we can calculate that the size of the mobile phone is 480/3 = 160 dpi, it is of low resolution, so under this resolution, a dip is equivalent to 1 px. How can this problem be calculated? Of course, 1dip * (160/160) = 1px. That is to say, when we are in this resolution of the mobile phone, the width and length of the control can be the same as the size of the design drawing, a dip corresponds to a px. However, if the resolution is the same, is it 2 inch * 3.3 inch? Then we can first calculate that the cell phone density is 480/2 = 240 dpi, which is a high resolution. In this resolution, a dip is equivalent to 1.5 pixels. How can we calculate it? Of course, 1dip * (240/160) = 1.5px.

Therefore, by understanding the relationship between dip and dpi, We can precisely set the control size.

Specifically, when the screen resolution is 320xlarge, the density is set to 160 dpi regardless of the actual physical size, which means that a row can be placed at most 320dp on this device, for 4.3x240 devices, the screen size ranges from 3 to, and the density is set to dpi. A row can also be placed in 320dp. Therefore, in development, you can use dp as the unit and set it once to adapt to multiple screens.

Now that we know the relationship between the two terms, how can we convert them in the program?

The following code helps us to convert dp to px

<Span style = "font-size: 18px;">/*** convert the unit from dp to px based on the resolution of the mobile phone) */public static int dip2px (Context context, float dpValue) {// obtain the multiples of the current screen density dpi and the basic density 160dpi, + 0.5f is used to ensure that the data converted to the int type is correct. float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f) ;}</span>

We can also convert px to dp.

<Span style = "font-size: 18px;">/*** Based on the cell phone resolution) convert the unit of to dp */public static int px2dip (Context context, float pxValue) {// obtain the multiples of the current screen density dpi and the basic density 160dpi, + 0.5f is used to ensure that the data converted to the int type is correct. float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f) ;}</span>




Differences between DPI and DIP

There is no connection between the two.
DIP is an incorrect word and short for other professional words, but it has nothing to do with the computer field

DPI usually refers to the resolution. Full name: Dots Per Inch is the point Per Inch

Inside the mouse is the update rate.
In printers and scanners, how many points can be identified in 1 inch.

How can I change the DIP resolution of an image?

Image option image 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.