Android Multi-screen adapter for DP and PX relationships
People have always asked me, Android multi-screen adaptation in the end is how to do, I do not know how to explain clearly, perhaps I am also quite confused.
The following conclusions are mainly based on the analysis of the official documents Https://developer.android.com/guide/practices/screens_support.html
Android due to fragmentation is too serious, and led to a very large number of kinds of mobile phone devices on the market, of course, also includes very wonderful resolution phone. So in the layout of the use of PX as a unit is obviously not very good to do multi-screen adaptation. In fact, in the official documentation there is a solution to the problem of multi-screen adaptation dimension Unit Dp/dip (density-independent pixel )
The relationship expression between PX and DP is also given in the document: PX = DP * (dpi/160) where DPI represents the device screen density, and different device dpi may not be the same.
For example, in a device screen with a DPI of 240, 1 DP is equivalent to 2 px, that is, a DP length contains 2 px, and if the ratio of DP to PX is larger, the screen will look clearer.
Google has already categorized DPI:
- A set of six generalized densities:
- ldpi (Low) ~120dpi
- mdpi (Medium) ~160dpi
- hdpi (high) ~240dpi
- xhdpi (Extra-high) ~320dpi
- xxhdpi (Extra-extra-high) ~480dpi
- xxxhdpi (Extra-extra-extra-high) ~640dpi
The above statement shows that he is an interval, not a fixed value.
Based on the relationship between PX and DP. If on a device with a screen pixel of 800*1280,dpi 240, the total length of the DP is only 600DP
On a device with a screen pixel of 800*1280 and a DPI of 320, the total length of the DP is only 400DP
Here is the test code to verify the actual length of the DP
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "Vertical" tools:context= ". Mainactivity "> <imageview android:id=" @+id/imageview "android:layout_width=" Wrap_content "a ndroid:layout_height= "Wrap_content" android:background= "#f00" android:src= "@drawable/ic_launcher"/> & Lt;imageview android:layout_width= "100dip" android:layout_height= "40dip" android:src= "#0f0"/> <imageview android:layout_width= "200dip" android:layout_height= "40dip" android:src= "#0fa"/> <imageview android:layout_width= "300dip" android:layout_height= "40dip" android:src= "#0ff"/> <imageview android:layout_width= "400dip" android:layout_height= "40dip" android:src= "#09f"/>& Lt;/linearLayout>
720*1280 320dpi 480*800 160dpi 480*800 240dpi
The total length of the DP with a total length of 360DP DP is 320DP of total length of 480DP DP
Summarize a
In general, if the screen resolution and screen density in a certain proportion of the relationship, regardless of the resolution, the resulting DP total length will be within a certain range. Then using DP as a unit will be a good solution to the problem of multi-resolution adaptation.
However, since the device screen resolution and screen density is not a fixed proportion, so all with DP write, it does not seem very good. It is recommended to use Warp_content and match_parent. For the layout of the aspect is not explicitly limited, as far as possible to combine the relative layout and weight to represent, so no matter what resolution, the interface style will not be garbled.
Android Multi-Screen adapter DP and PX are best used with DP