Explanation of common measurement units (xdpi, hdpi, mdpi, and ldpi) on Android

Source: Internet
Author: User
Android on the common measurement unit [xdpi, hdpi, mdpi, ldpi] interpretation from: http://www.cnblogs.com/cmduan/archive/2012/03/09/2388345.html

Explanation of common measurement units (xdpi, hdpi, mdpi, and ldpi) on Android

Terms and concepts
Screen Size
The physical size of the screen, based on the diagonal length of the screen (such as 2.8, 3.5 ).
In short, Android simplifies all screen sizes into three categories: Large, normal, and small.
The program can provide three different layout schemes for these three screens, and then the system will be responsible for rendering your layout scheme to the corresponding screen in an appropriate way, this process does not require code intervention by programmers.

Screen Aspect Ratio
The ratio of the physical length of the screen to the physical width. The program can provide materials for the screen with a aspect ratio. You only need to use the resource categories provided by the system, long and notlong.

Resolution 
Total number of pixels on the screen. Note that although the resolution is expressed as "width x length" in most cases, the resolution does not mean the aspect ratio of the screen. In Android systems, the program generally does not directly process the resolution.

Density 
Pixels arranged along the length and width of the Screen Based on the screen resolution.
Low-density screens have only a small number of pixels in the long and wide directions, while high-density screens usually have many-or even very many-pixels arranged in the same area. The screen density is very important. For example, the interface elements (such as a button) defined in pixels with the length and width are very large on low-density screens, but on a high-density screen, it will appear very small.

Density-independent pixels (DIP) 
A pixel in an abstract sense. A program uses it to define interface elements. As a unit unrelated to the actual density, it helps programmers build a layout scheme (width, height, and position of the interface element ).
A density-independent pixel is logically consistent with a pixel on a screen with a pixel density of DPI. This is also the default display device assumed by the Android platform. When running, the platform uses the density of the target screen as the benchmark to "transparently" the necessary DIP scaling operations. To convert density-independent pixels to screen pixels, use the following simple formula: pixels = dips * (density/160 ). For example, on a screen with DPI 240, one DIP equals 1.5 physical pixels. We strongly recommend that you use DIP to define the interface layout of your program, because this ensures that your UI can be normally displayed on the screen of various resolutions.

To simplify the programmer's troubles in various resolutions, and to enable platforms with various resolutions to run these programs directly, the Android platform classifies all screens by density and resolution, they are divided into three types:
· Three major dimensions: Large, normal, and small;
· Three different density types: high (hdpi), medium (mdpi), and low (ldpi ).DPI is the abbreviation of dot per inch. The number of dots per inch .]
If needed, the program can provide different resources (mainly layout) for screens of various sizes, or provide different resources (mainly Bitmap) for screens of various density ). In addition, the program does not need to deal with the size or density of the screen. During execution, the platform automatically loads the corresponding resources based on the size and density characteristics of the screen and extracts them from the logical pixels (DIP, used to define the interface layout) convert to physical pixels on the screen.

 

 

About Android nodpi, xhdpi, hdpi, mdpi, and ldpi

First, there are several basic concepts:

1. Screen size

That is, the actual size of the display screen is measured based on the diagonal line of the screen.

For the sake of simplicity, Android divides all the screen sizes into four types: small, normal, large, and super large (corresponding to small, normal, large, and extra large ).

Applications can provide different custom screen la s for these four dimensions-the platform will select the corresponding layout for rendering based on the actual screen size, which is transparent to the program side.

2. screen Aspect ratio

The aspect ratio is the ratio of the physical width to the physical height of the screen. Applications can use limited resources to provide screen layout resources for the specified aspect ratio.

3. Screen Resolution

The total number of physical pixels displayed on the screen. Note that although the resolution is usually expressed in width x height, the resolution does not mean the specific aspect ratio of the screen.

In the Andorid system, the application does not directly use the resolution.

4. Density

Specify the number of pixels that can be displayed within the physical width and height range based on the pixel resolution.

In the same wide and high area, the low-density display displays less pixels, while the high-density display displays more pixels.

Screen density is very important, because when other conditions remain unchanged, a total of wide and high Fixed UI components (such as a button) appear very large on the low-density display, on a high-density display screen, it looks small.

For simplicity, Android divides all screen resolutions into four sizes: small, common, large, and ultra-large (corresponding to small, normal, large, and extra large ).

Applications can provide different resources for these four dimensions-the platform transparently scales the resources to adapt to the specified screen resolution.

5. Device independent pixel Density-independent pixel (dp)

Applications can be used to define virtual pixel units of the UI component and describe the layout size and position in a density-independent manner.

A device independent pixel is equivalent to a physical pixel on a 160 dpi screen.

When the program is running, the system transparently processes independent pixel units of any devices that need to be scaled according to the actual density of the screen. The conversion of independent pixels of devices into actual pixels of the screen is very simple: pixels = dps * (density/160 ).

For example, on a 240 dpi screen, an independent device pixel equals 1.5 physical pixels. to ensure that the UI components can be properly displayed on different screens, we strongly recommend that you use device independent pixel units to define your application UI.

Four types of screen sizes: small, normal, large, and xlarge

Four density classifications: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)

It should be noted that xhdpi is added from Android 2.2 (API Level 8.

Xlarge is a category added from Android 2.3 (API Level 9.

DPI is the abbreviation of "dot per inch". The number of dots per inch.

Normal screen: ldpi is 120, mdpi is 160, hdpi is 240, and xhdpi is 320.

Reference: http://developer.android.com/images/screens_support/screens-ranges.png

Two methods to obtain screen resolution information:

DisplayMetrics metrics = new DisplayMetrics ();

Display display = activity. getWindowManager (). getdefadisplay Display ();

Display. getMetrics (metrics );

// The pixel value obtained here is the device independent pixel dp

// DisplayMetrics metrics = activity. getResources (). getDisplayMetrics (); The obtained parameter information is incorrect. Do not use this method.

Android. content. res. Resources. getSystem (). getDisplayMetrics () cannot be used (). The obtained width and height are empty.

 

Private void initResolutionStr (Context context ){
If (ApiConfig. getResolutionStr () = null | ApiConfig. getResolutionStr (). equals ("")){
WindowManager winMgr = (WindowManager) context. getApplicationContext (). getSystemService (Context. WINDOW_SERVICE );
Display display = winMgr. getDefaultDisplay ();
Int height = display. getHeight ();
Int width = display. getWidth ();
String resolution = height> width? Height + "x" + width: width + "x" + height;
ApiConfig. setResolutionStr (resolution );
// DensityDpi = 120 dpi is ldpi, densityDpi = 160 dpi is mdpi,
// DensityDpi = 240 dpi is hdpi, densityDpi = 320 dpi is xhdpi
DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
Int densityDpi = dm. densityDpi;
ApiConfig. setDensityDpi (densityDpi );
}
}

 

 

If you need to customize resource files for Android pad, the directory under the res directory may be:

Drawable

Drawable-ldpi

Drawable-mdpi

Drawable-hdpi

Drawable-xhdpi

Drawable-nodpi

Drawable-nodpi-1024 × 600

Drawable-nodpi-1280 × 800

Drawable-nodpi-800 × 480

Values

Values-ldpi

Values-mdpi

Values-hdpi

Values-xhdpi

Values-nodpi

Values-nodpi-1024 × 600

Values-nodpi-1280 × 800

Values-nodpi-800 × 480

Common measurement units on Android:
Px (pixel): the point on the screen, absolute length, related to hardware.
In (INCHES): the unit of length.
Mm (mm): the unit of length.
Pt (lbs): 1/72 inch, point.
Dp (density-independent pixels): An abstract unit based on screen density. 1dp = 1px on a display at 160 o'clock per inch.
Dip: Density-independent pixel, which is the same as dp.
Sp: Based on dp, it has nothing to do with the proportion. I personally think it is a vector graphics unit.

Reasons for the introduction of dp/dip:
In the past, programmers often designed computer user interfaces in pixels. For example, define a form field with a width of 300 pixels. the spacing between columns is 5 pixels, And the Icon size is 16 × 16 pixels. The problem with this processing is that if you run the program on a new display with a dot (dpi) higher per inch, the user interface will look small. In some cases, the user interface may be small to difficult to see the content. The measurement units irrelevant to resolution can solve this problem.

How to calculate Density(Please refer to original post: http://www.devdiv.com/thread-28610-1-1.html );
1. The standard is 240*320 painted on 1.5*2 square inch. For example, each square inch has 240*320/(1.5*2) = 25600 points, that is, the pixel of a square inch is 25600, so dpi is the square root of 160; if your dpi is 120, the density is 0.75.
2. density is not only related to width, but also to height. Therefore, whether width is 1.8 or 1.3, its density may be 1. For example, width is 1.8, as long as its height is 3/1. if pixel is 240*320, its density is still 1. If width is 1.3, as long as its height is 3/1. if the pixel is 240*320, the density is also 1.
3.320*480/(1.5*2) Get the point in the unit square inch as 51200, so the unit square inch is 240*320 draw twice the screen in 1.5*2. But this is square inch. We need to square it when calculating the density. So it should be two square meters. It is 1.414 square meters. The approximate density is 1.5.

How to achieve density-independent:
If the screen density is 160, dp and sp are the same as px. 1dp = 1sp = 1px, but if px is used as the unit, if the screen size remains unchanged (assuming it is still 3.2), the screen density is changed to 320. The original TextView width is set to 320 PX, And the 3.2-inch screen with a density of 160 is half shorter than the 3.2-inch screen with a density. But if it is set to 160dp or 160sp. The system automatically sets the width property value to 320px. That is, 160*320/160. Among them, 320/160 can be called the density proportion factor. That is to say, if dp and sp are used, the system will automatically convert according to the screen density change. The formula summarized in the official document is pixels = dps * (density/160 ).

Appendix:
It is said that the iPhone/Mac design has taken into account the support for any resolution from the very beginning. All the interface elements of iOS are already vectorized images, and the UI interface is system-level and has nothing to do with density; android supports any resolution, but it is not global. Verify the 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.