Android multi-resolution and multi-density interface adaptation solution

Source: Internet
Author: User

Preface

At the beginning of Android's design, the UI was adapted to multiple platforms. It provided a complete adaptation mechanism and became more accurate with the development of the version, UI adaptation is mainly influenced by two factors: screen size (screen width and pixel height) and screen density. Different adaptation schemes are used for different application scenarios, this document is only applicable to Android4.0 and earlier versions.

 

Related Concepts

Resolution:The number of pixels on the screen. For convenience, the pixel width (horizontal pixel number) of the screen is multiplied by the pixel height, for example, 1280x720. Otherwise, the screen resolution is 1280x720, the pixel width is not necessarily 1280

Screen density:The number of pixels in an area. dpi is usually used as the unit, that is, the number of pixels per inch.

Px:Length unit, in pixels

Dp:The length unit is independent of the specific screen density. The length is converted to the corresponding pixel Length Based on the screen density of the specific platform. The specific conversion rules are as follows: 1dp = (target screen density/Standard density) * px. The standard density is 160 dpi. For example, 1dp Length indicates the length of one pixel on a platform with a density of dpi, the 1.5 DPI platform indicates the length of pixels.

Screen Size:The screen size, usually expressed by the diagonal length of the screen

Android interface Adaptation Mechanism

The UI adaptation on different platforms is affected by the screen size and screen density. The Android adaptation mechanism adds limitations on these two factors after the resources, and distinguishes the resources on different platforms based on different limits, when using resources, Android will give priority to the resources that meet the requirements of the platform, find the nearest condition, and then find the default value (that is, no limit ), select resources suitable for the current platform to adapt to different platforms.

 

The screen sizes include small, normal, large, and xlarge, which respectively indicate small, medium, large, and ultra-large screens.

The screen density is divided into ldpi, mdpi, hdpi, and xhdpi. Their standard values are 120 dpi, 160 dpi, 240 dpi, and 320 dpi.

The preceding Division indicates a range:

 

After adding the above restrictions to the Resource Directory, you can specify a specific applicable platform for the resource, as shown below:

 

Indicates a large screen. In the medium density layout, select the main. xml above, and the ultra-large screen. In the medium density layout, select the main. xml below.

 

In actual development, the screen size is not intuitive enough. android converts the screen size to a resolution representation. You can select a qualifier based on the specific screen resolution.

 

Summary: by adding the above limits, you can adapt an apk to several mainstream screen sizes and screen density. This limitation method is more suitable for publishing applications and does not know the specific terminal parameters, however, it cannot be precisely adapted, and the two platforms with little screen size and density differences cannot be well differentiated.

 

In order to solve the above problem, since Android3.2, precise adaptation has been introduced. Theoretically, a platform that can adapt to any pixel width, height, and screen density needs to be added using the following methods

 

W1280dp indicates that the screen width is 1280dp, h752dp indicates that the screen height is 752dp, and 160dpi indicates the screen density. The screen width and height must be in the unit of dp, you can use the formula 1dp = (target screen density/Standard density) * px to convert the pixel width and height to the dp unit.

For example, the screen width and height of a platform are 1920px and 720px, and the screen density is 240 dpi.

The limitations for adapting to this platform are:

 

Or

 

According to the formula 1dp = (240/160) px = 1.5px, the width and height are converted to the dp units: 1280dp and 480dp, respectively.

 

 

 

 

1. Create multiple layout folders (the same is true for drawable ).

In
Create multiple layout folders under the res directory, and the folder name is layout-800x480. To adapt to that resolution, write it as anything.

Note:

A. Large numbers should be written in front: such as layout-854x480 rather than layout-480x854.

B. The two digits are preceded by the lowercase letter x, rather than the multiplication number.

2. Adjust the length and width of layout and other settings under layout. To adapt to different resolutions.

3. Add the following section in AndroidManifest. xml, which cannot be implemented without self-adaptation:

</Application>


<Supports-screens
Android: largeScreens = "true"
Android: normalScreens = "true"
Android: anyDensity = "true"/>


</Manifest>


Add the above Code between the </application> label and the </manifest> label. You can.

 


Android can adapt layout)


I. Different layout


The screen sizes of Android mobile phones vary by 480x320,640x360,800 x. How can an App automatically adapt to different screens?
In fact, it is very simple, just need to create different layout folder under the res directory, such as layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size.


Ii. hdpi, mdpi, and ldpi


In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.


Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:


(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)


The system will find the corresponding images in these folders according to the machine resolution.


Correction: it should be an image corresponding to different density


To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.


[I] Note: the resolution of the three items is different. Just as you lower the resolution of your computer, the image will become larger. On the contrary, the resolution will be high and the image will be reduced. [/I]
Screen direction:


Automatic switching of landscape and portrait screens:


You can create two directories: layout-port-800x600 and layout-land under the res directory, which are placed in the vertical and horizontal layout files, in this way, the system automatically calls the corresponding layout file when the screen direction of the mobile phone changes, to avoid the problem that one layout file cannot meet the two screen display conditions.


Automatic switching of landscape and portrait screens with different resolutions:


Take 800x600 as an Example
Two directories of layout-port-800x600 and layout-land-800x600 can be created under the res directory


Do not switch:


The following steps are circulated on the Internet, but I used to implement this configuration through a graphical interface, which is the same in different ways. I will paste the image when I have time.


Note: Each activity has this screenOrientation attribute. You must set this attribute for each activity. You can set this attribute to a portrait screen (portrait) or a non-gravity sensor (nosensor ).


Solution to keep the program interface in one direction and not change with the orientation of the mobile phone:

Configure AndroidManifest. xml. Add this line to android: screenOrientation = "landscape ".
For example, (landscape is horizontal and portrait is vertical ):

Java code:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifestxmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. ray. linkit"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". Main"
Android: label = "@ string/app_name"
Android: screenOrientation = "portrait">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity android: name = ". GamePlay"
Android: screenOrientation = "portrait"> </activity>
<Activity android: name = ". OptionView"
Android: screenOrientation = "portrait"> </activity>
</Application>
<Uses-sdk android: minSdkVersion = "3"/>
</Manifest>


In addition, the Activity is restarted every time the screen is switched on in android. Therefore, you should save the status of the current Activity before the Activity is destroyed and load the configuration when the Activity is created again, in progress, the game will not automatically restart!


Some programs are suitable for switching from a portrait screen to a landscape screen, or vice versa. What should we do at this time? You can configure android: screenOrientation = "portrait" in the Activity configuration area ". In this way, you can ensure that the portrait screen is always portrait or landscape.


Some programs are suitable for switching between landscape and landscape screens. What should we do? First, you must configure the Activity as follows: android: configChanges = "keyboardHidden | orientation". In addition, you must override the onConfigurationChanged method of the Activity. The implementation method is as follows:

@ Override
Public void onConfigurationChanged (Configuration newConfig ){
Super. onConfigurationChanged (newConfig );
If (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE ){
// Land do nothing is OK
} Else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT ){
// Port do nothing is OK
}
}

Write a program that supports multi-resolution, developed based on 1.6, and creates three resource folders drawable-hdpi drawable-mdpidrawable-ldpi, it stores 72*72 48*48 36*36 icon files. When I test on G1 (1.5 of the system), the icon should be adaptive to 48*48, but the actual display is 36*36. How can we make it adaptive to the icon of 48*48?

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.