Android resolution adaptation and android resolution

Source: Internet
Author: User

Android resolution adaptation and android resolution

The problem of Android resolution adaptation has always been a major problem for Android. Here, I refer to the official development documentation and some of the resolution processing skills in actual development to share with you.

Official resolution adaptation documentation "SupportingMultiple Screens"

Overview of Screens Support
  • Screen size: Screen size
  • Screen density: Screen density
  • Orientation: Direction
  • Resolution: Resolution
  • Density-independent pixel (dp or dip): similar to pixels, it is the smallest unit of granularity that makes up an image. It is equivalent to a physical pixel on a 160dpi screen.
Range of screens supported

Android 1.6 (API Level 4) supports multi-resolution adaptation and divides the screen size and density:
* Screen size: small, normal, large, xlarge
* Screen density: ldpi (low), mdpi (medium), hdpi (high), xhdpi (extra high)

Here, the size and density are based on the first Android T-Mobile G1 (HVGA Screen), so to reduce the product adaptation cost, you only need to consider the adaptability on generailized size/density.

In actual Layout design, the Unit should be dp, which can avoid the impact caused by different screen dpi. The most typical example is Apple's retina screen.

  • Xlarge: at least 960dp x 720dp
  • Large: at least 640dp x 480dp
  • Normal: at least 470dp x 320dp
  • Small: at least between DP x 320dp

Px = dp * (dpi/160)

For example, for a 1.5 DPI screen, 1dp is equivalent to pixels.

Px, dpi, dp, dip, sp
Measurement Unit Explanation
Px (pixel) Pixel unit
Dpi (dots per inch pixel density) The number of pixels per inch. Assume that the device resolution is 320*240, the screen length is 2 inch-1.5 inch, dpi = 320/2 = 240/1. 5 = 160.
Dp (density) Number of records per ordinary inch Density = Resolution/Screen size
Dip (Device-independent pixel, Device independent pixel) Similar to dp, different devices have different display effects. This is related to the hardware of the device. We recommend that you use this function to support WVGA, HVGA, and QVGA without pixels. The corresponding formula of dip and specific pixel values is dip value = device density/160 * pixel value. It can be seen that 1px = 1dip on a device with dpi (pixel density) of 160dpi
Sp (ScaledPixels pixel amplification) It is mainly used for font display (best for textsize ). According to google's suggestion, it is best to use sp as the unit for the font size of TextView, and check the source code of TextView. Android uses sp as the font size unit by default.
Screen Density Dpi Resolution Matching Group
QVGA Density = 1, 0.75 Densittydpi = 120 QVGA (240*320) Ldpi
HVGA Density = 1, 1.0 Densittydpi = 160 HVGA (320*480) Mdpi
VGA Density = 1, 1.0 Densittydpi = 160 VGA (480*640) Mdpi
WVGA Density = 1, 1.5 Densittydpi = 240 WVGA (480*800) Hdpi
WQVGA Density = 1, 2.0 Densittydpi = 120 WQVGA (240*400) Xhdpi
Why should we consider dpi when designing the UI?

Here is an example. If you put a 32*32 px button on the interface without considering dpi, it will be 3.7 in size, mobile phones with resolutions of 480*320, 600*480, and 800*600 are displayed as follows:

As shown in the figure, as the dpi of the device increases, the physical length of 1 px is reduced on the device, and the final display effect is that the component looks smaller.

Dpi is used as the unit of page dimension.

Label the widget size as 32dip * 32dip to obtain the following display results:

It can be seen that the display effect is much better. By default, Android uses density to match resource files, rather than the screen size. When the image resolution does not match the screen size, the system requires a large amount of additional memory to stretch and scale the image, these additional overhead will reduce the program running efficiency. The most critical issue is memory overflow exceptions. This is caused by the android memory recycle mechanism. When a large number of images are used, the probability of occurrence is quite high. Currently, there is no effective solution.

Get density and dip

Mobile phone density and DIP can be obtained using the following methods: getWindowManager, getResources, and the same result, but for some reason, there may be some differences. Some mobile phone manufacturers may modify the API, while some manufacturers may make low-end screens intentionally display high-end data. If the screen data you obtain is not accurate, you do not have to worry too much about it:

Void getdefadisplaydisplayscreensize () {DisplayMetrics dm = newDisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); int screenWidthDip = dm. widthPixels; // screen width (dip, for example, 320dip) int screenHeightDip = dm. heightPixels; // screen width (dip, for example, 533dip) float density = dm. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) int densityDPI = dm. densityDpi; // screen density (pixels per inch: 120/160/240/320) float xdpi = dm. xdpi; float ydpi = dm. ydpi; w = dm. widthPixels; // screen width (px, for example, 480px) h = dm. heightPixels; // screen height (px, for example, 800px)} void getdefadisplaydisplayscreendensitydpi () {DisplayMetricsdm = newDisplayMetrics (); getResources (). getDisplayMetrics (); int screenWidthDip = dm. widthPixels; // screen width (dip, for example, 320dip) int screenHeightDip = dm. heightPixels; // screen width (dip, for example, 533dip) float density = dm. density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0) int densityDPI = dm. densityDpi; // screen density (pixels per inch: 120/160/240/320) float xdpi = dm. xdpi; float ydpi = dm. ydpi ;}
Screen adaptation process

1. Declare the application-compatible screen size
In the manifest file, it is used to declare the application-compatible screen size. This allows the device to determine whether the Screen Compatibility Mode is used to run the application.

2. provide different Layout s for different screen sizes
The layout files are stored in the layout-small, layout-normal, layout-large, and layout-xlarge folders.

3. provide different image resources for different dpi
If dip is used to mark the size of the component, the image resources measured in pixels are scaled to adapt to the size of the component on the screen, it is easy to cause issues such as blurred display and corner deformation, so different image resources will be prepared for different dpi. If you have prepared different image resources for different dpi devices, we recommend that you perform ldpi, mdpi, and, resources on hdpi and xhdpi. For more information about android, see IconDesign Guidelines"

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.