(Conversion) drawable (ldpi, mdpi, hdpi, xhdpi, and xxhdpi) in the android res folder ),
Drawable (ldpi, mdpi, hdpi, xhdpi, and xxhdpi) in the android res folder)
(1) The drawable-hdpi contains high-resolution images, such as WVGA (480x800) and FWVGA (480x854)
(2) drawable-mdpi stores medium-resolution images, such as HVGA (320x480)
(3) drawable-ldpi stores low-resolution images, such as QVGA (240x320)
Ldpi: 240x320
Mdpi: 320x480 hdpi: 480x800, 480x854xhdpi: At least 960*720 xxhdpi: 1280x720
Android adaptive multi-screen support
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.
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. Get screen pixels and screen density in the 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 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.
You are welcome to repost this article, but please indicate the author and Source:
Author: meteor
Source: http://blog.sina.com.cn/staratsky