Common units of measure on Android "xdpi, hdpi, mdpi, ldpi" interpretation

Source: Internet
Author: User
Tags square root

Terminology and concepts
Screen size
The physical size of the screen, based on the diagonal length of the screen (e.g. 2.8-inch, 3.5-inch).
In short, Android simplifies all screen sizes into three main categories: big, Normal, and small.
The program can provide three different layout schemes for these three sizes of screens, and then the system will be responsible for rendering your layout scheme to the corresponding screen in the appropriate way, this process does not require the programmer to intervene with the code.

Screen aspect ratio
The ratio of the physical length of the screen to the physical width. The program can provide the developed footage for the aspect ratio of the screen, using only the system-provided resource classifier long and Notlong.

resolution
The total number of pixels that are on the screen. Note that although the resolution is expressed in most cases as "width x length", the resolution does not imply a screen aspect ratio. In Android, programs generally do not deal directly with resolution.

density
Pixels that are based on the screen resolution and are arranged in a long, wide direction along the screen.
Low-density screens have fewer pixels in both the long and wide directions, and high-density screens usually have a lot of-even very, very many-pixels in the same area. The density of the screen is very important, for example, a long-width, pixel-defined interface element (such as a button) that appears large on a low-density screen, but small on a high-density screen.

density-independent pixels (DIP)
Refers to an abstract pixel, which the program uses to define the interface elements. It is a unit that is independent of the actual density and helps programmers build a layout scheme (width, height, and position of interface elements).
A density-independent pixel, on a logical size, that is consistent with a pixel on a screen with a pixel density of 160DPI, which is the default display device assumed by the Android platform. At run time, the platform takes the density of the target screen as a benchmark and "transparently" handles all required dip scaling operations. To convert density-independent pixels to screen pixels, you can use a simple formula: pixels = dips * (density/160). For example, on a screen with a dpi of 240, 1 dips equals 1.5 physical pixels. We strongly recommend that you use dip to define the layout of your program's interface, as this will ensure that your UI will display properly on screens of all resolutions.

To simplify the programmer's face in various resolutions, but also for the various resolutions of the platform can directly run these programs, the Android platform to all the screen with density and resolution of the classification, each divided into three categories:
• Three main sizes: large, normal, small;
• Three different densities: high (hdpi), Medium (MDPI) and Low (LDPI). "DPI is the abbreviation for" dot per inch ", pixels per inch. 】
If needed, the program can provide different resources (mainly layouts) for various sizes of screens, or it can provide different resources (mostly bitmaps) for screens of various densities. In addition, the program does not require any additional processing for the size or density of the screen. When executed, the platform automatically loads the corresponding resources according to the size and density characteristics of the screen itself, and converts them from logical pixels (dips, which define the interface layout) to physical pixels on the screen.

About Android nodpi,xhdpi,hdpi,mdpi,ldpi

The first is a few basic concepts:

1. Screen size

That is, the actual size of the display screen, measured according to the diagonal of the screen.

For simplicity, Android puts all screen sizes into four sizes: small, normal, large, oversized (corresponding to: small, normal, large, and extra large).

The application can provide different custom screen layouts for each of these four sizes-the platform will be rendered according to the actual size of the screen, which is transparent to the program side.

2. Screen aspect ratio aspect ratio

The aspect ratio is the proportional relationship between the physical width of the screen and the physical height. An application can provide a screen layout resource for a specified aspect ratio by using a qualified resource.

3. Screen resolution resolution

The sum of the physical pixels displayed on the screen. It is important to note that although the resolution is usually expressed as a wide x-height, the resolution does not imply a specific aspect ratio of the screen.

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

4. Density density

Depending on the pixel resolution, the number of pixels displayed in the screen specifies the physical wide-height range.

In the same wide-height area, low-density displays can display fewer pixels, while high-density displays can display more pixels.

Screen density is very important, as other conditions are constant, a total width and height of the fixed UI components (such as a button) on the low-density display is very large, and on the high-density display is very small.

For simplicity, Android puts all the screen resolutions into four sizes: small, normal, large, oversized (corresponding to: small, normal, large, and extra large).

Applications can provide different resources for each of these four sizes-the platform will transparently scale the resources to match the specified screen resolution.

5. Device independent pixel density-independent pixel (DP)

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

A device-independent pixel is equivalent to a physical pixel on a single, DPI screen.

When the program is running, the system transparently handles any device that needs scaling based on the actual density of the screen, and the conversion of the device independent pixel to the actual pixel on the screen is simple: pixels = DPS * (density/160).

For example, on a three-dpi screen, 1 device independent pixels equals 1.5 physical pixels. To ensure that the UI components are displayed properly on different screens, it is highly recommended to use device-independent pixel units to define your application UI.

Four Screen size categories:: Small, normal, large, and XLarge

Four density classifications: ldpi (Low), MDPI (Medium), hdpi (High), and xhdpi (extra high)

It is important to note that XHDPI is the only category that has been added since Android 2.2 (API level 8).

XLarge is the category that was added from Android 2.3 (API level 9).

DPI is the abbreviation for "dot per inch" and the number of pixels per inch.

General screen: LDPI is 120,mdpi is 160,hdpi is 240,xhdpi is 320.

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

Two ways to get screen resolution information:

Displaymetrics metrics = new Displaymetrics ();

Display display = Activity.getwindowmanager (). Getdefaultdisplay ();

Display.getmetrics (metrics);

The pixel value obtained here is the device independent pixel DP

Displaymetrics metrics=activity.getresources (). Getdisplaymetrics (); The parameter information obtained is incorrect and should not be used in this way.

You cannot use Android.content.res.Resources.getSystem (). Getdisplaymetrics (). This gets the width and height is 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 = 120dpi is ldpi, densitydpi = 160dpi is mdpi,
densitydpi = 240dpi is hdpi, densitydpi = 320dpi is xhdpi
Displaymetrics dm = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
int densitydpi = dm.densitydpi;
APICONFIG.SETDENSITYDPI (DENSITYDPI);
}
}


If you need to customize the resource file for Android pad, the directory under the Res directory might be:

Drawable

drawable-ldpi

drawable-mdpi

drawable-hdpi

drawable-xhdpi

drawable-nodpi

drawable-nodpi-1024x600

drawable-nodpi-1280x800

drawable-nodpi-800x480

Values

values-ldpi

values-mdpi

values-hdpi

values-xhdpi

values-nodpi

values-nodpi-1024x600

values-nodpi-1280x800

values-nodpi-800x480

Common units of measure on Android:
PX (pixels): The point on the screen, absolute length, related to hardware.
In (inches): unit of length.
MM (mm): unit of length.
PT (LB): 1/72 inch, point.
DP (density-independent pixels): An abstract unit based on screen density. 1DP = 1px on a monitor 160 dots per inch.
Dip:density-independent pixel, same as DP.
SP: On the basis of DP, but also proportional to the scale, personal understanding is a vector graphics units.

reasons for introducing Dp/dip:
In the past, programmers typically designed computer user interfaces in pixels. For example, you define a form field with a width of 300 pixels, a space between columns of 5 pixels, an icon size of 16x16 pixels, and so on. The problem with this approach is that if you run the program on a new monitor with a higher dots per inch (dpi), the user interface will appear small. In some cases, the user interface may be too small to see the content. Resolution-independent units of measure can solve this problem.

how to calculate density(Please refer to the original post: http://www.devdiv.com/thread-28610-1-1.html);
1. The standard is 240*320 painted on the 1.5*2 square inch. So like every square inch has 240*320/(1.5*2) = 25,600 points, that is, a square inch pixel point is 25600, so the DPI takes its square root 160, if your DPI is 120, then its density is 0.75.
2. Density is not only related to width, but also to height, so whether width is 1.8 or 1.3, its density may be 1, such as width is 1.8, as long as its height is 3/1.8, if Pixel is 240*320, Its density is still 1, and if the width is 1.3, as long as its height is 3/1.3, the pixel is 240*320, then the density is 1.
3.320*480/(1.5*2) Gets the point of the unit square inch is 51200, so the unit square inch is the 240*320 painted on the 1.5*2 screen twice times. But this is square inches ah, calculate the density of the time to open the square ah, so should be 2 open square, is 1.414 bar, the approximate density of 1.5.

How to do it with density Independent:
If the screen density is 160, then the DP and SP and PX are the same. 1DP=1SP=1PX, but if you use PX as the unit, if the screen size is constant (assuming 3.2 inches), the screen density becomes 320. So the original width of the TextView is set to 160px, in the density of 320 of the 3.2-inch screen is more than the density of 160 of the 3.2-inch screen to see the half-short. But if it is set to 160DP or 160SP. The Width property value is automatically set to 320px. That means 160 * 320/160. 320/160 of these are called density scale factors. That is, if you use the DP and SP, the system will automatically convert based on the change in screen density. The Official document summary is calculated as: pixels = DPS * (density/160).


attached:
Legend Iphone/mac Design from the outset to consider the support of arbitrary resolution, iOS all the interface elements are already vectorized images, UI interface is system-level and density-independent, while Android support arbitrary resolution, but not the system global, verify.

Xdpi, hdpi, MDPI, ldpi for common units of measure on Android

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.