0. Foreword
Android's screen fit, even if an element has the same display effect on Android's different sizes and resolution phones, has been a problem that we Android developers have to face. This article refers to a lot of previous blogs, and to make a summary of this issue, and strive to streamline and understand.
Reprint please indicate the source: http://blog.csdn.net/seu_calvin/article/details/52690498
1. Basic Concepts
(1) screen size, that is, the cell phone diagonal physical size
1 inches = 2.54cm Common mobile phone size is 5 inches, 5.5 inches, 6 inches and so on.
(2) screen resolution, that is, the cell phone in the horizontal, vertical pixel points sum (generally described as the screen "width x high")
For example, 1080DPX1920DP, which has 1080 pixels in the width direction, has 1920 pixels in the height direction, and 1px=1 pixels.
Common resolutions for Android phones: 320x480, 480x800, 720x1280, 1080x1920
(3) screen pixel density, that is, pixel points per inch, unit 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, 400 of which is derived from the wide-high pixel Pythagorean theorem.
Android phone according to Pixel density, can be divided into the following types of screen density:
(4) density independent pixel, the unit is DP, is the Android Special Unit
Android usually uses a DP instead of PX units to set the size of the picture, because it ensures that the same effect is displayed on devices with different screen pixel densities.
/**
* DP and PX Conversion
* Created by Seu_calvin on 2016/09/28/
public
class Densityutils {public
static int DP2PX (float DP, context context) {
float density = context.getresources (). Getdisplaymetrics (). density;//is 0.75 of the table 1/1.5/2/3
//context.getresources (). Getdisplaymetrics (). densitydpi//Table 120/160/240/320/480 return
(int) (DP * density + 0.5f);
}
public static float PX2DP (int px, context context) {
float density = context.getresources (). Getdisplaymetrics (). Density;
Return px/density
}
}
(5) independent proportional pixel, unit for the unit used to set the text size when sp,android development
Can be scaled according to the font size preference, it is recommended to use 12/14/18/22SP as the size of the font set, do not recommend the use of odd and decimal, easy to cause loss of precision.
This article introduces the basic concepts above, and then we introduce the solution of screen adaptation from three angles of layout adaptation, picture adaptation, and code adaptation.
2. Layout matching
(1) It is recommended to use relative layouts to disable absolute layout. The relative position of the view does not change as the relative layout changes the size of the screen.
(2) Use DP and SP (as far as possible without PX), wrap_content, match_parent and weight to control the layout. The right to use weight weight on any device will be perfect fit.
(3) design different layouts for different screen sizes, and configure qualifiers to enable the program to automatically load the appropriate layout resources according to the size of the current device at run time.
For example, we first write two layout files, respectively:
Adaptation of mobile phone layout (default): Res/layout/main.xml
Fit size >7-inch flat layout: Res/layout/main_pb.xml
Then add the following two files, and the system will automatically choose which layout profile to use according to the Android version.
Fit with Android 3.2 flat layout
res/values-large/layout.xml
<resources>
<item name= "main" type= " Layout "> @layout/main_pb</item>
</resources>
//fit with Android 3.2 flat layout
res/ Values-sw600dp/layout.xml
<resources>
<item name= "main" type= "layout" > @layout/main_pb</ Item>
</resources>
The above two configuration files do not really define the layout, they simply set main to the @layout/main_pb alias.
If this is not done, the contents of the Main_pb.xml layout file need to be duplicated into two parts, respectively, into Res/layout-large/main.xml and res/layout-sw600dp/main.xml to fit 3.2 before and after, which is obviously redundant.
3. Picture Fitting
(1) For example, there is a demand that the background image of a button must be able to change as the size of the button changes. Using a normal picture will not be able to do this because the runtime will evenly stretch or compress your pictures.
At this point, you can use the Nine-patch diagram (a specially processed PNG image, using the. 9.png suffix name), the 9Patch chart can specify the stretch area and the non stretch area of the picture, and when you need to stretch the picture, the system automatically stretches the part you want to stretch. It should be noted that. 9 figure does not require multiple resolution pictures, placed in the Drawable folder.
Red Box area: an area that represents a portrait stretch, that is, when a picture needs to be stretched vertically, it specifies only the stretch red area.
Green Box Area: an area that represents stretching horizontally, that is, when a picture needs to be stretched horizontally, it specifies only the stretch green area.
(2) Pictures do not need to be in the following figure hdpi, MDPI and other directories are placed in the corresponding resolution of the picture, this will make apk larger, generally only to do 1280*720 a set of figures, placed in the hdpi or xhdpi, if the problem and then for the screen to replace the problem picture. In addition to how to reduce the size of apk, you can refer to Android development-reduce apk size.
3. Code Matching
(1) For example, there is a need to achieve a space width, is the screen of 1/3. This time you can implement it in code:
/**
* Code fit Example
* Created by Seu_calvin on 2016/09/28
/WindowManager wm = Getwindowmanager ();
int width = Wm.getdefaultdisplay (). getwidth ();
Linearlayout.layoutparams params = (layoutparams) tv.getlayoutparams ();
Params.width = WIDTH/3;
Tv.setlayoutparams (params);
It should be noted that general code adaptation needs to write a tool class (already posted above) to implement DP2PX, because the parameters in the code generally require PX value, the need for the different device screen density to achieve dp2px.
(2) Another use scenario for code adaptation is to decide to go through different processes depending on the load layout, as follows:
Setcontentview (r.layout.main_layout); This will load the different layout
Button btn = (button) Findviewbyid (R.ID.BTN) depending on the screen size;// One of the layouts does not have the button
if (btn = null) {
//business logic 1 ...
} else{
//Business logic 2 ...
}
Thank you for reading, I hope to help you, thank you for your support for this site!