How the Android system adapts to the screen size

Source: Internet
Author: User

1. Screen Related concepts
1.1 Resolution
It means how many pixels are there on the screen.
1.2 Screen Size
Refers to the actual physical size of the phone, such as the usual 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
Pixels per inch
The phone can have the same resolution, but the screen size can be different,
Diagonal pixel represents the pixel value (=) of the diagonal, dpi=933/3.7=252
Android divides the actual screen density into four universal sizes (Low,medium,high,and extra high)
General screen: LDPI is 120dpi,mdpi is 160dpi,hdpi is 240dpi,xhdpi is 320dpi
For the screen, the larger the DPI, the higher the fineness of the screen, the more clearly the screen will look
1.4 Density-independent pixels (density-independent Pixel--dip)
Dip is a virtual unit of pixels
The corresponding formula for dip and specific pixel values is dip/pixel=dpi value/160, which is PX = DP * (dpi/160)
When you define the UI for your app's layout, you should use DP units to ensure that the UI is displayed correctly on different screens.

The corresponding relationship between cell phone screen classification and pixel density is shown in table 1

Size distribution of mobile phones (http://developer.android.com/resources/dashboard/screens.html),



At present, mainly in the resolution of 800*480 and 854*480 mobile phone users mostly
From the above screen size distribution, in fact, the phone just consider 3-4.5-inch density of 1 and 1.5 of mobile phones

2. Android multi-screen support mechanism
Android's support for multi-screen mechanism is used to provide an appropriate way for the current device screen to co-manage and resolve application resources.
The Android platform supports a range of appropriate resources for the specified size (size-specific), specifying Density (density-specific) that you provide.
The appropriate resource for the specified size (size-specific) refers to small, normal, large, and XLarge.
The appropriate resource for specifying density (density-specific) refers to ldpi (low), MDPI (Medium), hdpi (High), and xhdpi (Extra high).
Android has an automatic matching mechanism to select the corresponding layout and picture resources
1) Interface layout aspect
Prepare 5 sets of layouts according to the size of the physical size:
Layout (Put some common layout XML files, such as the top and bottom layout of the interface, will not change with the screen size, like the WinDOS window title bar),
Layout-small (the screen size is less than 3 inches or so),
Layout-normal (screen size is less than 4.5 inches or so),
Layout-large (between 4 inches-7 inches),
Layout-xlarge (between 7-10 inches)
2) Picture Resources
5 sets of picture resources need to be prepared according to the DPI value:
Drawable: Primary placement of XML configuration files or images with lower resolution requirements
DRAWALBE-LDPI: Low-resolution images, such as QVGA (240x320)
DRAWABLE-MDPI: Medium-resolution picture, such as HVGA (320x480)
DRAWABLE-HDPI: High-resolution pictures, such as WVGA (480x800), FWVGA (480x854)
DRAWABLE-XHDPI: At least 960DP x 720DP

Android has an automatic matching mechanism to select the corresponding layout and picture resources.
The system will be based on the resolution of the machine to each of these folders to find the corresponding picture.
In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements.

3. Androidmanifest.xml Configuration
Ndroid from 1.6 and higher, Google has added auto-fit functionality to facilitate the porting of various resolution models to developers
<supports-screens
Android:largescreens= "true"
Android:normalscreens= "true"
Android:smallscreens= "true"
Android:anydensity= "true"/>
3.1 Support for multiple screens with different densities
Android:anydensity=["true" | "False"]
If android:anydensity= "true"
The application supports different densities and automatically matches according to the screen resolution.
If android:anydensity= "false"
The application supports different densities, and the system automatically scales the image size and coordinates of the image. Explain how the system automatically scales resources.
For example, if we have the same resource under the Hdpi,mdpi,ldpi folder, then the application will not automatically go to the appropriate folder to find resources, this situation is in high-density, and low-density mobile phones, such as a 240x320 pixel cell phone,
If you set android:anydensity= "false", the Android system will convert 320 (low density) to 320x480 (medium density), so the app will load the resources in the mdpi file on the small-density phone.
3.2 Whether to support large screen
Android:largescreens=["true" | "False"]
If you declare a large screen that is not supported, and this screen size is larger, the system uses a size of ("normal") and a density of ("medium) display,
However, a black background will appear.
3.3 Support for small screens
Android:smallscreens=["true" | "False"]
If you declare a small screen that is not supported, and the current screen size is smaller, the system also uses a size of ("normal") and a density of ("medium) display
This property is not required if the application can be scaled correctly on a small screen (minimum small size or minimum width of 320DP). Otherwise, you should set this property for the minimum screen width identifier
To match the minimum size required by the application.

4, Android provides 3 ways to handle screen adaptive
4.1 Pre-scaled resources (looking for pictures based on size and density)
1) If you find the appropriate size and density, use these images for a non-scaling display.
2) If the size cannot be found and the density is found, the image size is considered "medium" and the image is displayed using zoom.
3) If they do not match, the default picture is used to scale the display. Default image default Standard "medium" (160).
4.2 Auto-scaled pixel dimensions and coordinates (density compatible)
1) If the application does not support different densities android:anydensity= "false", the system automatically scales the image size and coordinates of this picture.
2) for pre-scaled resources, when android:anydensity= "false", it does not take effect.
3) android:anydensity= "false", only for density compatibility function, size compatibility no effect
4.3 compatible with larger screens and sizes (size compatible)
1) for large screens that you do not support, and this screen size is normal, the system uses the size ("normal") and density ("medium") to display.
2.) for large screens that you do not support in the declaration, and this screen size is larger, the system also uses dimensions ("normal") and density ("medium") to display,
However, a black background will appear.

5, Android system automatic adaptation skills
The Android system uses the following two ways to automate the application:
1) When defining the length in the layout file, it is best to use wrap_content,fill_parent, or DP, to ensure that the screen is displayed with the appropriate size
2) for different screen density of mobile phones, provide different bitmap resources, can make the interface clear and non-scaling.
For bitmap resources, automatic scaling sometimes causes the zoomed-out image to become blurred, which requires applications to provide different resources for different screen density configurations: high-definition images for dense screens, etc.
3) do not use Absolutelayout
4) Use Dip for pixel units, use SP for text units

6. Get screen pixel, screen density in code
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 method and its shortcomings
7.1 Image Zoom
Based on the accuracy of the current screen, the platform automatically loads any non-scaled, dimensioned and precision pictures. If the picture does not match, the platform loads the default resources and, after zooming in or out, satisfies the current interface's display requirements. For example, for a high-precision screen, the platform loads high-precision resources (such as bitmap resources in drawable-hdpi in helloandroid), and if not, the platform scales medium precision resources to high precision, resulting in unclear picture display.
7.2 Automatically define pixel dimensions and locations
If the program does not support multiple precision screens, the platform automatically defines the pixel absolute position and size values, so that the element can show the same size as the accuracy of 160 on the screen. For example, to let the WVGA high-precision screen and the traditional HVGA screen display the same size picture, when the program is not supported, the system will lie to the screen resolution of 320x480, after the (10,10) to (100,100) in the area of the drawing is completed, the system will enlarge the graph to (15, 15) to the (150,150) screen display area.
7.3 Compatible with larger screen sizes
When the current screen exceeds the upper limit of the screen supported by the program, the Supportsscreens element is defined so that the platform displays a black background image when it exceeds the displayed baseline. For example, WVGA in the precision screen, such as the program does not support such a large screen, the system will be lied to be a 320x480, the extra display area will be filled with black.
7.4 Dynamic drawing of images with OpenGL
The Android bottom layer provides OpenGL interfaces and methods to dynamically draw pictures, but this is a great challenge for developers unfamiliar with computer graphics. General development game, using OpenGL mode.
More than 7.5 apk files
Symbian and the traditional j2me in this way, for an application to provide multiple resolution versions, users according to their own needs to download and install the appropriate executable files. It's a good idea to develop apps separately for each screen, but Google market's support for multiple resolution versions of an application is not perfect at the moment, and developers need to use an APK file to accommodate multiple resolutions whenever possible.

This article welcomes the reprint, but please indicate the author and source:

Meteor

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

How the Android system adapts to the screen size

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.