Android screen adaptation issues in a detailed

Source: Internet
Author: User

previous -android localization Resource Directory detailed :http://www.cnblogs.com/stafen/p/3833048.html

Unit:

PX (pixels): The point on the screen.

In (inches): unit of length.

MM (mm): unit of length.

PT (lb): 1/72 inches.

Dp/dip (density-independent pixels): An abstract unit based on screen density. On a monitor that is 160 dots per inch, 1DP = 1px may increase on a monitor larger than 160 points. Units that are typically used for location and dimension properties.

DPI: Indicates the density of the current screen.

SP (scale independent pixels): Primarily for font size units.

Identifier:

Screen Size: The identifiers available are small, normal, large, xlarge.

Screen Aspect: The available identifiers are long, Notlong.

Screen oriention: The available identifiers are port, land.

Screen pixel density (pixel density [dpi]): The available identifiers are ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi.

Minimum screen width (smallestwidth): identifier styles are SW<N>DP, such as SW320DP, SW600DP, SW720DP.

The width of the screen that can be used (Available width) has an identifier style: W<N>DP, such as W720DP, W1024DP.

The screen height that can be used (Available height) has an identifier style: H<N>DP, such as H720DP, H1024DP.

Localized Resource Catalog case:

Res/ayout/main_layout.xml//layout for normal screen size, also the default layout

Res/layout-small/main_layout.xml//Layout for small screen size

Res/layout-large/main_layout.xml//Layout for large screen size

Res/layout-xlarge/main_layout.xml//Layout for XLarge screen size

Res/layout-xlarge-land/my_layout.xml//For XLarge screen size and layout in landscape direction

Res/drawble-mdpi/icon.png//image for medium-density screen

Res/drawble-port-hdpi/icon.png//For high-density screens, the MDA screen orientation is portrait image

Res/drawble-xhdpi/icon.png//image for extended high-density screen

First, the screen adaptation of the main considerations

Screen size

Screen density

Screen orientation

Screen resolution

Pixel (DP) independent of screen density

1. Screen size

The actual physical size of the screen, which is the length of the screen diagonal. For convenience, Android will divide the physical dimensions of each screen into the following 4 types:

Small

Normal

Large

Extra Large

2. Screen density

The physical pixel dots per inch of the screen, usually expressed in DPI. For example, a medium-density screen is less dense than a high-density screen per inch of pixels, so the screen is not high-density in the display screen.

To facilitate Android, the various actual screens are divided into the following 4:

Low density (lo density)

Medium density (medium dendity)

High density (hi density)

Extended density (extra high density)

3. Screen orientation

The mobile phone can be rotated in 2 to 4 directions by means of a direction sensor. This is required for the program interface to support horizontal screen (landcape) and vertical screen (portrait) layout. The usual practice for both layouts is to provide two different layout files. However, you can also run the Ganzhi screen direction of the transformation, and adjust the layout of the screen in a timely manner.

4. Screen resolution

The number of physical pixels across the screen, usually expressed in N*m, n for horizontal resolution and M for portrait resolution. In earlier versions of Android, the configuration identifiers that used direct screen resolution were supported, but it was not allowed to use the screen resolution directly as a configuration identifier in a later version of Android. Screen size and screen density approximations are typically used to represent the resolution of the screen, which means a set of approximate resolutions for a size or screen density.

5. Pixel (DP) independent of screen density

DP is a unit that is independent of screen density and is typically used to set the location and size of the UI layout. The DP corresponds to the physical pixel of the 160dpi screen, that is, if the current screen is medium density, then the DP is equivalent to PX (pixels). If the current screen is not medium density, the actual physical pixels are calculated using the following formula so that the relative position and dimensions of the UI remain constant on different densities of screens.

PX = DP * (dpi/160)

Where DPI represents the density of the current screen. For example, the current TextView control has a width of 160DP and the program runs on a high-density screen phone with a DPI of 240 (240 physical pixels per inch), then the actual physical pixel of the TextView control is 240px, and is calculated as follows:

PX = 160 * (240/160) = 240

From this point can be seen, in the high-density screen, 1DP = 1.5px, two low-density (120 per inch of physical pixels) screen, 4dp=3px.

Second, the adaptation of different screens can be used by the rules

1. Limit screen Size

If your application is designed specifically for a certain type of screen, for example, a large level screen, the simplest way is to go directly to the Androidmanifest.xml file through <compatibe-screens> or < The supports-screens> label displays the screen size. These two tags make it impossible to find your program on Google Play on a phone that doesn't meet your screen requirements.

2. Provide different layouts for different screen sizes

①android version 3.2: You can consider the size and density of your screen. For example, the screen size is divided into 4 levels: small, normal, large, and XLarge. If you provide the appropriate layout resources for these 4 dimension classes, you need to create 4 layout resource catalogs: Res/layout-small, Res/layout-normal, Res/layout-large, and Res/layut-xlarge.

②android 3.2 (API level=13): 3 new configuration specifiers are added: Minimum screen width, screen width, and screen height can be used. For example, the minimum width of the screen is represented by SW<N>DP, where N represents the minimum width value, and if you want to provide a layout file for an Android device with a minimum screen width of 600DP, you need to place the layout file in the RES/LAYOUT-SW600DP directory.

3. Provide different resolution graphics for the density of different screens

By default, Android stretches normal graphics (. png,. jpg, and. gif files) and Nine-patch pictures, but for different screen densities, the image's stretch can cause distortion. As a result, images that are richer in color often provide a corresponding resolution for each screen density. Android currently divides the screen density into 4 levels: ldpi (Low Density), mdpi (medium density), hdpi (high Density), and xhdpi (extended high density). For example, if you want to provide picture resources for both high-density and medium-density screens, you should create a res/drawable-mdpi and res/drawable-hdpi two localized image resource catalog. In general, if the system is to find the corresponding split wood resources directory, the use of the default resource directory res/drawable, or a more appropriate resource directory (usually select higher-order image compression).

Three, screen size and screen density

Starting with Android 1.6 (API level=4), Android has started to support multi-screen size and screen density adaptation, and in this way can overwrite all current screen configurations (no need to specify screen physical resolution directly as before).

Screen size (see previous page analysis);

The screen density is divided into 4 levels: ldpi (Low Density), mdpi (medium density), hdpi (high Density), and xhdpi (extended high density).

Screen size and screen density are based on normal and MDPI, respectively.

Each screen size and screen density applies to a range, not a fixed value, which means that although the dimensions of multiple devices are normal, their screen physical size and length ratios may vary. Is the irony of the screen size, screen density and the actual screen size, screen density of the corresponding relationship:

When you design your UI using screen size, you must understand the minimum screen length size required for each size. These minimum length and width dimensions use DP (pixels independent of screen density) as units. The above is the minimum screen length width for the above 4 generalization screen size.

Xlarge:960dpx720dp

Large:640dpx480dp

Normal:470dpx320dp

Small:426dpx320dp

The units of these dimensions are DP, which means that their corresponding screen physical pixels are relative to the screen size, but also to the screen density. For example, if the screen density is medium density, the DP is equivalent to PX, and all 470px≤h<640px, 320px≤w<480px screens (H and W indicate screen height and width respectively) are normal. For high-density screens, due to 1dp=1.5px, the minimum physical size required for large is 960pxx720px.

Now, for example, Nexus 7 and Nexus S, explain how the Android system calculates the screen size of a particular type of device. The Nexus 7 screen density is 213 (213 physical pixels per inch), for convenience, to directly convert the physical pixels of the screen to DP, so the screen size in DP units is 905DPX553DP (the Nexus 7 screen physical pixel size is 1205x736), The screen size of the Nexus 7 is large against the generalized screen size in the above 4. For Nexus S, the screen density is 240 (240 physical pixels per inch), and the Nexus S screen physical pixel size is 800x480, which is represented as 533DPX320DP with DP. From this point of view, Nexus s screen size is exactly between normal and large, more system-determined rules, this screen belongs to normal.

V. Important factors that affect screen adaptation: layouts and images

1. Layout screen Adaptation

Layouts can use screen density-independent units (DP and SP) to automatically adjust the UI, but if the screen is too small (such as a small-rated screen) or too large (such as a xlarge-rated screen), and the screen orientation changes, depending on the system auto-adjustment will make the rain boots control too small or too large, Significantly reduce user experience and display results. Therefore, in this clear sky, easy to provide a separate layout.

For small-rated screens, the UI layouts you might use are significantly different from those of the small and large levels because the screen size is too small. For example, because the zoom is too small, the Button becomes too small and the text above will not appear, in which case you need to adjust the UI layout separately for the small level screen.

For screens with XLarge ratings (sometimes large levels), although the software has no problem with the use of the auto-adjusted interface of the layout, the size of the various controls increases significantly, so in most cases the UI layout should be provided specifically for xlarge or large-level screens.

Screen will continue to widen and narrow as you switch between the screens and the screen. In many cases, because the screen's height or width is smaller, it turns out that the size of the control that is right for the display is smaller, and even some controls cannot be displayed (the courseware scope of the underlying screen), so if a large number of controls are placed horizontally or vertically on the screen, unless the width and height of those controls change the user's experience, Otherwise, you should provide a separate UI layout for the horizontal and vertical screens.

In summary, the UI layout should be provided separately for small, large, xlarge, and crossbar unless the size of the control changes without impacting the user experience.

2. Image Screen Device

By default, the system automatically stretches the image with a larger screen size. For normal images (. png,. jpg, or. gif), the entire image will be stretched, and for the dictation application, only a portion of the image can be stretched. To implement such a feature, you need to use an image in the Nine-patch (. 9.png) format.

For different densities of screens common to different resolutions of the image resource is the scaling principle that can be used 3:4:6:8. These 4 numbers are low-density, medium-density, high-density, extended-high-density image resolution ratios. For example, the resolution of the icons used by Android apps on the medium density screen is 48x48, so the image resolution for the 4 density levels is as follows:

36x36: Low density.

48X48: Medium density.

72x72: High density.

96X96: Extended high density.

Vi. Cases

To help with better screen adaptation, here are some classic screen widths:

320DP: typical mobile screen (e.g. 240x320ldpi, 320x480mdpi, 480x800hdpi, etc.);

480DP: Mobile phones and tablets are willing to use the size (480X800MDPI);

600DP: For a 7-inch Tablet PC (such as the Nexus 7 1280x800, between MDPI and hdpi).

720DP: For a 10-inch Tablet PC (such as Nexus 10 of 2560 wrong 1600, between hdpi and xhdpi).

If you use the minimum screen width, the 7-inch tablet and phone offer two different layout files (MAIN_ACTIVITY.XNL), which can be used directly with 600DP.

Res/layout/main_activity.xml //Layout file for the phone

Res/layout-sw600dp/main_activity.xml //Layout file for 7-inch Tablet PC

If you use the above two resource directories, only the resources in the RES/LAYOUT-SW600DP directory will be used when the screen can be used at a width greater than 600DP, and only a tablet screen of 7 inches or more can normally reach 600DP.

If you want to further differentiate the screen size, for example, to provide layout files for phones, 7-inch tablets, and 10-inch tablets, you will need to peak the corresponding Main_activity.xml files in the following 3 resource directory files.

Layout files for mobile phones with a minimum screen width of less than 600DP

Res/layout/main_activity.xml

Layout files for 7-inch tablets with a minimum screen width equal to 600DP, but less than 720DP

Res/layout-sw600dp/main_activity.xml

Layout files for 10-inch tablets with a minimum screen width greater than or equal to 720DP

Res/layout-sw720dp/main_activity.xml

Of course, in addition to the minimum screen width, you can also use the screen height and width of the screen to fit, using the same method as the minimum screen width.

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.