How does Android adapt to screen size?

Source: Internet
Author: User

1. Screen related concepts
1.1 resolution
The number of pixels on the screen.
1.2 screen size
It refers to the actual physical size of the mobile phone, such as the commonly used 2.8 inch, 3.2 inch, 3.5 inch, 3.7 inch
Android divides the screen size into four levels (small, normal, large, and extra large ).
1.3 screen Density
Number of records per inch
The cell phone can have the same resolution, but the screen size can be different,
Diagonal pixel indicates the pixel value of the diagonal line (=), DPI = 933/3. 7 = 252
Android divides the actual screen density into four general dimensions (low, medium, high, and extra high)
General screen: ldpi is 120 DPI, mdpi is 160 DPI, hdpi is 240 DPI, and xhdpi is 320 DPI
For the screen, the larger the DPI, the higher the fineness of the screen, the more clearly the screen looks
1.4 density-independent pixels (density-independent pixel -- DIP)
DIP is a virtual pixel unit.
The corresponding formula of dip and specific pixel values is dip/pixel = DPI value/160, that is, PX = DP * (DPI/160)
When you define the UI of the application layout, you should use the DP Unit to ensure that the UI is correctly displayed on different screens.
The relationship between mobile phone screen classification and pixel density is shown in table 1.
Cell phone size distribution (http://developer.android.com/resources/dashboard/screens.html ),
Currently, most mobile phone users have a resolution of 800*480 and 854*480.
From the above screen size distribution, we can only consider mobile phones with a density of 1 and 1.5 between 3-inches.
2. Android multi-screen Support Mechanism
Android supports the multi-screen mechanism, which provides an appropriate method for the current device screen to jointly manage and parse application resources.
The Android platform supports a series of suitable resources with the specified size and density.
The appropriate size-specific resources are small, normal, large, and xlarge.
Proper density-specific resources are ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high ).
Android has an automatic matching mechanism to select the corresponding layout and image resources.
1) interface Layout
Prepare five la s based on the physical size:
Layout (put some general layout XML files, such as the layout at the top and bottom of the interface, without changing the screen size, similar to the title bar of the S window ),
Layout-small (layout with a screen size less than 3 inch ),
Layout-normal (the screen size is less than 4.5 inch ),
Layout-large (between 4 inch and 7 inch ),
Layout-xlarge (between 7-10 inch)
2) image resources
You need to prepare 5 sets of image resources based on the DPI value:
Drawable: Mainly stores xml configuration files or images with low resolution requirements.
Drawalbe-ldpi: low-resolution image, such as qvga (240x320)
Drawable-mdpi: medium-resolution image, such as hvga (320x480)
Drawable-hdpi: high-resolution images such as WVGA (480x800) and fwvga (480X854)
Drawable-xhdpi: At least 960dp x 720dp
Android has an automatic matching mechanism to select the corresponding layout and image resources.
The system will find the corresponding images in these folders according to the machine resolution.
In development Program To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.
3. androidmanifest. xml configuration
Android from 1.6 to higher, Google adds automatic adaptation to facilitate developers to transplant various resolution models
<Supports-screens
Android: largescreens = "true"
Android: normalscreens = "true"
Android: smallscreens = "true"
Android: anydensity = "true"/>
Does 3.1 support multiple screens of different density?
Android: anydensity = ["true" | "false"]
If Android: anydensity = "true"
The application supports different density and will automatically match according to the screen resolution.
If Android: anydensity = "false"
The application supports different density. The system automatically scales the image size and coordinates of the image. The system automatically scales resources.
For example, if we have the same resource in the hdpi, mdpi, and ldpi folders, the application will not automatically search for resources in the corresponding folder, which is caused by high density, and low-density mobile phones, such as a 240x320 pixel mobile phone,
If Android: anydensity = "false" is set, the android system converts 240x320 (low density) to 320x480 (medium density, the application loads Resources in the mdpi file on a cell phone with a low density.
3.2 support for Large Screen
Android: largescreens = ["true" | "false"]
If a large screen is declared not supported and the screen size is larger, the system displays the screen in the ("normal") and density ("medium) sizes,
However, a black background may appear.
3.3 small screen supported
Android: smallscreens = ["true" | "false"]
If you declare a small screen that is not supported and the current screen size is smaller, the system also displays the screen with the size ("normal") and density ("medium)
This attribute is not required if the application scales properly on a small screen (small or 320dp. Otherwise, set this attribute for the minimum screen width identifier.
To match the minimum size required by the application.
4. Android provides three methods for screen adaptation.
4.1 Pre-scaled resources (search for images based on size and density)
1) if the corresponding size and density are found, the images are displayed without scaling.
2) if the corresponding size cannot be found, and the density is found, the image size is considered as "medium", and the image is displayed by scaling.
3) If none of them match, use the default image for Zoom display. The default image is standard with "medium" (160) by default ).
4.2 automatically scaled pixel sizes and coordinates (density compatibility)
1) if the app does not support Android: anydensity = "false", the system automatically scales the image size and coordinates of the image.
2) for pre-scaled resources, it does not take effect when Android: anydensity = "false.
3) Android: anydensity = "false". It only works for density compatibility, but does not work for dimension compatibility.
4.3 compatible with larger screens and sizes (size compatible)
1) if the screen size is normal for a large screen that is declared not supported by you, the system displays the screen size ("normal") and density ("medium.
2.) If the screen size is larger and you declare that it is not supported, the system will also display the screen size ("normal") and density ("medium,
However, a black background may appear.
5. Android automatic adaptation skills
Android uses the following two methods to automatically adapt applications:
1) when defining the length in the layout file, it is best to use wrap_content, fill_parent, or DP to describe it, so as to ensure a proper size for the display on the screen.
2) provide different bitmap resources for mobile phones with different screen density, so that the interface is clear without scaling.
For bitmap resources, automatic scaling sometimes results in blurred and enlarged images. Therefore, the application needs to provide different resources for different screen density configurations: provides high-definition images for high-density screens.
3) do not use absolutelayout
4) dip is used for pixel units and SP is used for text units.
6.CodeObtain screen pixels and screen Density
Displaymetrics metric = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (metric );
Int width = metric. widthpixels; // screen width (pixels)
Int Height = metric. heightpixels; // screen height (pixels)
Float density = metric. density; // screen density (0.75/1.0/1.5)
Int densitydpi = metric. densitydpi; // screen density DPI (120/160/240)
7. General Multi-Resolution Processing Methods and Their disadvantages
7.1 Image Scaling
Based on the precision of the current screen, the platform automatically loads any images with limited sizes and precision without scaling. If the image does not match, the platform loads the default resources and the display requirements of the current interface can be met after the image is zoomed in or out. For example, if the screen is a high-precision screen, the platform loads high-precision resources (such as the bitmap resources in drawable-hdpi in helloandroid). If not, the platform scales the medium-precision resources to a high-precision value, the image is not displayed clearly.
7.2 automatically defines the pixel size and position
If the program does not support multiple precision screens, the platform automatically defines the absolute position and size values of pixels, in this way, the elements can be displayed in the same size as those on the screen with an accuracy of 160. For example, to make the WVGA high-precision screen display the same size as the traditional hvga screen, when the program does not support it, the system will say that the screen resolution of the program is 320 × 480, after the drawing is completed in the (100,100) to (150,150) area, the system will enlarge the image to the (15, 15) to () screen display area.
7.3 compatible with screens of larger sizes
When the current screen exceeds the upper limit of the screen supported by the program, the supportsscreens element is defined, so that when the display baseline is exceeded, the platform displays a black background image here. For example, on a WVGA precision screen, if the program does not support such a large screen, the system will say that it is a 320 × 480 screen, and the excess display area will be filled with black.
7.4 use OpenGL to dynamically draw images
The android underlying layer provides OpenGL interfaces and methods to dynamically draw images. However, this method is a great challenge for developers who are not familiar with computer graphics. Generally, games are developed using OpenGL.
More than 7.5 APK files
This method is used by Symbian and the traditional J2EE. It provides multiple resolution versions for an application. You can download and install the corresponding executable files as needed. It is a good method to develop an application for each screen. However, Google market currently does not provide complete support for multiple resolution versions of an application, developers still need to use an APK file to adapt to multiple resolutions as much as possible.

Source: http://blog.sina.com.cn/staratsky

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.