Important Concepts
What is screen size, screen resolution, screen pixel density?
What is DP, dip, DPI, SP, px? What is the relationship between them?
What is MDPI, hdpi, xdpi, xxdpi? How to calculate and differentiate?
We will cover these concepts in the following sections.
Screen size
Screen size refers to the length of the diagonal of the screen, in inches, 1 inches = 2.54 centimeters
For example, common screen sizes are 2.4, 2.8, 3.5, 3.7, 4.2, 5.0, 5.5, 6.0, etc.
Screen resolution
Screen resolution refers to the number of pixels in the horizontal portrait, in px,1px=1 pixels. Generally in portrait pixels * transverse pixels, such as 1960*1080.
Screen pixel density
screen pixel density refers to the number of pixels per inch, in DPI, which is the abbreviation fordot per inch. Screen pixel density is related to screen size and screen resolution, in the case of single change, the smaller the screen size, the higher the resolution, the greater the pixel density, the lower the inverse.
DP, dip, DPI, SP, px
PX We should be more familiar with, the front resolution is in pixels, in most cases, such as UI design, Android native API will be the PX as a unified unit of measurement, such as to obtain a higher screen width.
Dip and DP is a meaning, are density independent pixels abbreviation, that is, density-independent pixels, as we said above, DPI is the screen pixel density, if there is 160 pixels in an inch, this screen pixel density is 160dpi, then in this case, How is DP and PX converted? in Android, the rule is 160dpi, 1dip=1px, if the density is 320dpi, then 1dip=2px, and so on.
if the same is to draw a 320px line, 480*800 resolution on the phone display as 2/3 screen width, on the 320*480 phone is full full screen, if using DP units, at both resolutions, 160DP is displayed as half the length of the screen. This is why, in Android development, you should try to use DP instead of PX when writing layouts.
The SP, or scale-independent pixels, is similar to DP, but can be indented based on the text size preference, which is the Queen's Unit for setting the font size.
MDPI, hdpi, xdpi, xxdpi
In fact, there is a ldpi, but as the mobile device configuration is constantly upgrading, this pixel density device is very rare, where it is now suitable for the need not to consider.
MDPI, hdpi, xdpi, xxdpi are used to modify the Drawable folder and the values folder in Android to distinguish between images and dimen values in different pixel densities.
So how do you differentiate it? Google's official designation is differentiated according to the following criteria:
| name |
pixel density range |
| mdpi |
120dpi~160dpi |
| hdpi |
160dpi~240dpi |
| xhdpi |
240dpi~320dpi |
| xxhdpi |
320dpi~480dpi |
| xxxhdpi |
480dpi~640dpi |
In the development, we need to put the appropriate size of the picture in the appropriate folder. The following is an example of an icon design.
When designing icons, the five main pixel densities (MDPI, HDPI, XHDPI, xxhdpi, and xxxhdpi) should be scaled according to the 2:3:4:6:8 scale. For example, the size of a boot icon is 48x48 DP, which means that on MDPI's screen its actual size should be 48x48 px, its actual size on the HDPI screen is 1.5 times times MDPI (72x72 px), and its actual size on the xdpi screen is MDPI 2 Times (96x96 px), and so on. Although Android also supports low-pixel density (ldpi) screens, it does not bother to do so, and the system automatically shrinks the HDPI size icon to 1/2 for matching.
The corresponding size for each screen density of the icon
| screen Density |
icon Size |
| mdpi |
48x48px |
| hdpi |
72x72px |
| xhdpi |
96x96px |
| xxhdpi |
144x144px |
| xxxhdpi |
192x192px |
Solution supports various screen sizes using wrap_content, match_parent, weight
To ensure the flexibility of the layout and to accommodate screens of various sizes, you should use "Wrap_content" and "match_parent" to control the width and height of some view components.
With "Wrap_content", the width or height of the view is set to the minimum size required to fit the content in the view, and "Match_parent" is called "fill_parent" at levels below the API level 8 ) expands the component to match the dimensions of its parent view.
If you use the "Wrap_content" and "match_parent" dimension values instead of the hard-coded dimensions, the view will use only the space or expansion that you want to fill the available space accordingly. This method allows the layout to properly accommodate various screen sizes and screen orientations.
weight is a unique property of linear layouts , and we can use this property to distribute the interface in proportion to achieve some special requirements.
Use relative layout to disable absolute layout
In development, we use linear layouts, relative layouts, and frame layouts most of the time, and the absolute layout is rarely used because of the poor fit.
Because of the different layout features are different, so you can not say which layout is good, in the end should use what layout can only be determined according to actual needs. We can use nested instances of linearlayout and combine "wrap_content" and "match_parent" to build a fairly complex layout. However, we cannot precisely control the special relationship of the sub-view through LinearLayout, and the system will list the views in linearlayout directly
If we need to arrange the sub-views in various effects instead of a straight line, it is often more appropriate to use relativelayout so that the layout can be specified based on the special relationship between the components. For example, we can snap a child view to the left side of the screen while aligning another view to the right side of the screen.
About Constraitlayout:
http://www.jianshu.com/p/792d2682c538
http://www.jianshu.com/p/32a0a6e0a98a
This digest is from:
Http://www.cocoachina.com/android/20151030/13971.html
Android Development resolution adaptation summary