Android phone screen adaptation solution

0. Preface

Android screen adaptation, even if a certain element has the same display effect on Android phones of different sizes and resolutions, this problem has always been a problem that we Android developers have to face. This paper refers to many previous blogs, and makes a summary of this problem, trying to be concise.

Please indicate the source for Reprint: http://blog.csdn.net/seu_calvin/article/details/52690498

1. Basic concepts

(1) Screen size, that is, the physical size of the diagonal of the mobile phone

1 inch = 2.54cm. Common mobile phone sizes are 5 inches, 5.5 inches, 6 inches, etc.

(2) Screen resolution, that is, the total number of pixels in the horizontal and vertical directions of the mobile phone (generally described as "width x height" of the screen)

For example, 1080dpx1920dp, i.e. 1080 pixels in the width direction and 1920 pixels in the height direction, 1px = 1 pixel

Common resolutions of Android Phones: 320x480, 480x800, 720x1280, 1080x1920

(3) Screen pixel density, that is, the number of pixels per inch, in dpi

For example, if a device is 240x320 and the screen size is 3.3 inches, the screen pixel density of the device is 400 / 3.3 = 120dpi, of which 400 is obtained by the width height pixel Pythagorean theorem.

Android phones can be divided into the following screen density types according to pixel density:

(4) Density independent pixel, in DP, is a unique unit for Android

When Android is developed, it usually uses DP instead of PX units to set the picture size, because it can ensure that the same effect can be displayed on devices with different screen pixel density.

(5) Independent scale pixel, the unit is SP, which is used to set the text size when Android is developed

It can be scaled according to the font size preference. It is recommended to use 12 / 14 / 18 / 22sp as the font size. Odd numbers and decimals are not recommended, which is easy to cause the loss of accuracy.

After introducing the above basic concepts, we will introduce the solutions of screen adaptation from three perspectives: layout adaptation, image adaptation and code adaptation.

2. Layout adaptation

(1) Relative layout is recommended and absolute layout is disabled. Because the relative layout does not change when the screen size changes.

(2) Use DP and SP (try not to use PX) and wrap_ content、match_ Parent and weight to control the layout. Using weight fits perfectly on any device.

(3) Different layouts are designed for devices with different screen sizes. By configuring qualifiers, the program automatically loads appropriate layout resources according to the size of the current device.

For example, let's write two layout files first, which are:

Adaptive phone layout (default): RES / layout / main xml

Layout of flat plate with adaptive size > 7 inches: RES / layout / main_ pb. xml

Then add the following two files, and the system will automatically select which layout configuration file to use according to the Android version.

The above two configuration files do not really define the layout. They just set main to @ layout / main_ Alias for Pb.

If not, main_ pb. The contents of the XML layout file need to be copied into two copies and placed in RES / layout large / main XML and RES / layout-sw600dp / main XML to fit before and after 3.2, which is obviously redundant.

3. Picture adaptation

(1) For example, there is a requirement that the background image of a button must be able to change with the size of the button. Using ordinary pictures will not achieve the above functions, because your pictures will be stretched or compressed evenly at run time.

At this time, you can use the nine patch graph (a specially processed PNG picture, using the. 9.png suffix). The 9patch graph can specify the stretched area and non stretched area of the picture. When you need to stretch the picture, the system will automatically stretch the part you want to stretch. It should be noted that. 9 images do not need multiple resolutions, but can be placed in the drawable folder.

Red box area: indicates the area of vertical stretching, that is, when the picture needs vertical stretching, it will only specify the red area of stretching.

Green box area: indicates the area stretched horizontally, that is, when the picture needs to stretch horizontally, it will only specify the stretched green area.

(2) Pictures do not need to be put into the corresponding resolution pictures in the following hdpi, MDPI and other directories, which will make the APK larger. Generally, only a set of 1280 * 720 pictures are made and placed under hdpi or xhdpi. If there is a problem, replace the problem pictures on the screen. In addition, for how to reduce the APK size, you can refer to Android Development - reducing the APK size.

3. Code adaptation

(1) For example, there is a requirement to achieve a space width, which is 1 / 3 of the screen. At this time, it can be implemented in Code:

It should be noted that for general code adaptation, a tool class (already posted above) needs to be written to implement dp2px, because the parameters in the code generally need PX values, and dp2px needs to be implemented through the screen density of different devices.

(2) Another use scenario of code adaptation is to decide to follow different processes according to different loading layouts, as follows:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>