Device compatibility for Android best practices

Source: Internet
Author: User
Tags comparison table

Screen resolution may vary due to the size of different phones. You may have encountered these problems while developing your application:

1, why does the picture become smaller or smaller when it is displayed on another phone?

2, why is the pattern defined in layout distorted on another phone?

3, why does your own custom control size display is not normal?

。。。。。。。。。。。。。。。。

These are some of the more common device compatibility issues in Android, but it's not difficult to solve these problems, as long as you have mastered some basic concepts and the Android internal processing compatibility question mechanism, these problems can be solved.

Some basic knowledge about size compatibility and some best practices are collected below, hoping to help you.

1, screen size and density reference value:

Note: 1 inch = 2.5cm

Density Baseline: 160dpi

2, screen size and density comparison table:

3,PX and DP conversion rules on different resolution devices:

One, if the size of the device is 2 inches, the resolution is 320, over there its density is 160dpi, that is, the basic reference line. At this time: 1PX=1DP=1SP.

Second, if the size of the device is 2 inches, the resolution is 480, then its density is 240/dp. At this time: 1.5PX=1DP=1SP

Conversion rule px = DP (dpi/160).

This resolves why DP is used as a unit of length, and the physical length displayed on different devices remains consistent even when the screen resolution changes. When using PX as a unit, it becomes smaller in high-resolution devices and becomes larger in low-resolution devices.

4, use Wrap_content, fill_parent, or DP to define the size of the layout.

The advantage of using DP to define the dimensions of a component is that the size of the dimension is automatically converted based on the DPI of the device. For example, on a machine with a density of 160dpi, if you define a component with a size of 100DP, the actual size of the display is 100px. On a machine with a density of 240, the system displays the size of px=100 * (240/160) px= 150px. This conversion can be achieved at different resolutions of the device, for the same component, the physical size of the display is the same, does not appear to become larger or smaller.

5, different density devices corresponding to the optimal ratio of image files.

For four density low-dpi, medium-dpi, high-dpi, extra high-hdpi devices, when specifying an identical image file, the size of the image file assigned to various densities should conform to the following proportions: 3:4:6:9. That is to meet the density ratio (120:160:240:360).

For example, if we were to use a 48 * 48 image file on a device with a density of 160dpi. So for other density devices, the image files we want to prepare are:

Low-Density (120dpi): 36x36

Medium-density (160dpi): 48x48

High-density (240dpi): 72x72

High-density (360dpi): 96x96

6. Provide the specified layout file for each size device, if required.

Res/layout/my_layout.xml//layout for normal screen size ("Default")

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

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

Res/layout-xlarge/my_layout.xml//Layout for extra large screen size

Res/layout-xlarge-land/my_layout.xml//layout for extra large in landscape orientation

7. Provide different image files for each density device, if required.

Res/drawable-mdpi/my_icon.png//bitmap for medium density

Res/drawable-hdpi/my_icon.png//bitmap for high density

Res/drawable-xhdpi/my_icon.png//bitmap for extra high density

8, do not use PX in layout file.

Because using PX will show different physical dimensions on machines with different resolutions.

9, do not use the absolutelayout layout.

Because absolute layouts require absolute offsets, problems can occur on machines of different sizes.

, 10, how to let us write our own controls can be configured for length and adaptive for all devices:

In all custom controls, do not hard-code length, width, and so on, should be based on the baseline 160dpi mode to get the control under the standard baseline control of the best value, if set to width. Then pass density = getresources (). Getdisplaymetrics (). Density gets the scaling value of the current device relative to the baseline, and then gets an optimal value = width * density. Use this value to draw the UI of the control. This allows us to change the size of the control at any time in the configuration file.

Note: density = getresources (). Getdisplaymetrics (). Density

This value returns different values in devices of different densities:

160dpi:1

240dpi:1.5

120dpi:0.75

.... analogy

Device compatibility for Android best practices

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.