Reprint Please specify source: http://blog.csdn.net/my_truelove/article/details/66584865
Visit ruicb.com, one click to reach my blog! Scan the left or the bottom right QR code, follow my public number, timely access to the latest article push!
- Conceptual Literacy inchpx and DPI
- 1 inch of screen size
- 2 px of screen resolution
- DPI of 3 pixel density
- Fragmentation of dip with SP
- 1 density-independent pixel dip
- 2 scaling independent pixels of SP
- From H XH xxh.
- The use of layout weight
- Mutual conversion of PX and DP
1 concept of literacy inch, px and dpi1.1 screen size inch
Whether it is a TV, tablet, or mobile phone, the size of the screen refers 屏幕对角线的长度
to the unit is inches (inch), 1 inches equivalent to 2.54 centimeters;
That1 inch ≈ 2.54 cm
1.2 PX of screen resolution
The screen resolution means 设备屏幕上的像素个数
that the side length of each pixel is 1px, which is the smallest unit that makes up the phone's screen.
Often said phone resolution is 1920*1080, that is, the phone has 1080 pixels in the landscape, vertical has 1920 pixels. With the same screen size, the higher the resolution, the finer and more delicate the display will be.
Note: In Android development, data related to the View dimension (width height), position (coordinates) is ultimately measured and plotted in PX units.
DPI of 1.3 pixel density
Pixel density, expressed in dpi, Dot-per-inch, as the name implies 每英寸上的像素点数
.
像素密度
and closely related to the previous mention 屏幕尺寸
屏幕分辨率
, make sure to understand the previous content and then continue to learn.
Give me a chestnut.
For example, Google Pro son Nexus 5, with a screen size of 4.95 inch and a resolution of 1920 * 1080, has a pixel density of:
√ (19202 + 10802)/4.95 (√ stands for open radical)
Note: The resolution is 1920 * 1080, which means that the portrait and landscape are composed of 1920 and 1080 pixel points respectively, then the pixel point of the screen diagonal is √ (19202 + 10802), and the screen size (that is, the diagonal length) is 4.95 inch, so the pixel points on the unit feet are: √ (19202 + 10802)/4.95 = 445, which is the pixel density.
2 fragmentation of dip with SP
Before you begin, talk about the commonplace Android fragmentation problem.
Android's Open source, let the major vendors flock to, a variety of custom hardware and ROM, resulting in the fragmentation of Android devices serious. Cell phone size and resolution is a reflection of the hardware layer fragmentation: the same size of the phone screen, the resolution may be different, different or resolution of the same phone, screen size and size vary.
Developers simply can't fit the UI with a single factor (size or resolution), so the layout doesn't work well on every phone.
See this, you estimate whispered: This can do? Don't worry, Google has provided us 密度无关像素
with a 缩放无关像素
variety of resolution-related issues and continue to look.
2.1 Density-independent pixel dip
Density-independent pixels, in dip (referred to as DP, hereinafter), that is, density-independent pixels, as the name implies, the unit and the phone's screen pixels do not have an absolute relationship.
Note: Be careful to differentiate between the DP and the DPI mentioned here.
So, DP is the best unit to fit the screen in development. When you write a layout in XML, you can directly use DP units, and in Java code you can convert the DP to PX for adaptation.
As I said earlier, all dimensions and widths in development are measured in px, so what is the conversion between the DP and PX we are using? Keep looking down with questions.
With this reminder, make sure you understand the previous concepts. In order to better learn the knowledge behind, let me again wordy review:
- Inch Size: The length of the diagonal of the screen;
- PX of resolution: pixel points in vertical and horizontal direction;
- DPI of pixel density: pixel points per inch;
After reviewing, we continue to look at the relationship between PX, DPI and DP, knocking the blackboard to focus on:
The conversion between DP and PX depends on the DPI of the current device.
Well, that's the point. Don't excite your classmates, put down your shoes, let me raise a chestnut.
In the case of Nexus 5, which is said earlier, its dpi≈445, at this time 1DP = 3px. Here's 3 of course not I was blindfolded, there is a comparison table:
120dpi-160dpi 1DP = 1px
160dpi-240dpi 1DP = 1.5px
240dpi-320dpi 1DP = 2px
320dpi-480dpi 1DP = 3px
480dpi-640dpi 1DP = 4px
Classmate, do not worry first back conversion multiples, DP and PX conversion multiples in the development can be obtained through the API, we will say later.
2.2 Scaling independent pixels of SP
Scaling independent pixels, or scale-independent pixels (SP), to reduce the difficulty of comprehension, so to speak:
The SP and DP can be understood as a unit of measure of the same type, unlike the DP used to measure size and width, and the SP is used to measure the size of the font.
The DP can be converted to different PX to fit the screen depending on the screen density, and the SP can be scaled according to the text size preference to fit the effect of different screens.
From the basic inch, px and dpi, chat to Android fragmentation, and then to the fragmented solution DP and SP, the basic knowledge is introduced, the back of the digest, say how to use.
3 from H, XH, Xxh.
In the development of the image resources, in order to adapt to the screen, we often put a set of resources in different drawable
directories, so that at different DPI devices, the program is encoded, the corresponding drawable
directory of resources will be used. The following is the table (by the way, the conversion of DP and PX is integrated):
DPI |
drawable |
dp/px |
120dpi-160dpi |
mdpi |
1 |
160dpi-240dpi |
hdpi |
1.5 |
240dpi-320dpi |
xhdpi |
2 |
320dpi-480dpi |
xxhdpi |
3 |
480dpi-640dpi |
xxxhdpi |
4 |
4 Layout weight Usage
Weight represents weights, we know that weights are a relative concept, so what does it do in the layout?
Suppose there is a need for this:
-Two buttons on the landscape, each half of the screen width, how to write?
-or three buttons, each occupying a screen width of one-third, how to write?
-or two buttons, the right side of the adaptive, left to occupy the remaining area?
May as well write, the space is limited does not raise chestnuts, such requirements use weight implementation will be very simple. Note the properties of the control in the code android:layout_weight
, which determines the weight of the control.
The first one:
<linearlayout android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:orientation = "horizontal" ; <button android:layout_ Width = "0DP" android:layout_height =< Span class= "Hljs-value" > "wrap_content" android:layout_weight = "1" android:background = " @color/colorprimary " android:text =" button 1 " /> <buttonandroid:layout_width="0DP"android:layout_height="Wrap_ Content "android:layout_weight=" 1 "android:background=" @color/coloraccent " Android:text="button 2" /> </linearlayout>
The effect is as follows:
The second one is similar to the first one, no longer an example, see the third one:
<linearlayout android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:orientation = "horizontal" ; <button android:layout_ Width = "0DP" android:layout_height =< Span class= "Hljs-value" > "wrap_content" android:layout_weight = "1" android:background = " @color/colorprimary " android:text =" button 1 " /> <buttonandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:background="@color/coloraccent"android:text="button 2" /> </linearlayout>
The effect is as follows:
Looking at the example code above, you might be wondering, here's how Android is based on the weight layout.
In fact, the use of weight is very simple, after understanding is a sentence, with the width discussed above as an example:
The actual width of the View = the value of the Layout_height setting + the remaining width of the parent layout * Layout_weight weight
In the first example, the width of button 1 and button 2 Layout_width = 0, the weight layout_weight = 1, assuming the screen width is W, sets the formula.
Then the width of button 1 = 0 + (w-0-0) * 1/(1 + 1) = W/2.
which
- W-0-0 Indicates the remaining width of the parent layout because two button widths are 0;
- 1/(1 + 1) represents the weight of the button 1 in the remaining width, because the weight of button 1 is 1,button 2 with a weight of 1, so the total weight is 1 + 1.
The button 2 width is calculated as Button 1, and the result is W/2. As you can see, button 1 and button 2 each account for half of the screen width W.
A third example is understood in the same way:
The width of button 2 layout_width = "wrap_content"
, the width is unknown, that is assumed to be w ', the screen width is still w.
The width of button 1 is:0 + (w-0-W ') * 1/(1 + 0) = W-w '
The calculation is no longer explained, the final result can be seen, regardless of the button 2 width how adaptive, Button 1 and button 2 width is W, also implemented the button 1 fills the remaining area effect.
Note: Weight only works in LinearLayout, is limited to a lot of space is not expanded, more use of tips can refer to the Android Develop website or other blog, it is worth learning.
5 px vs. DP conversion
When the SP, DPI, and DP relationships are described earlier, the ratio of DP to PX conversion at different DPI is mentioned. So how do you get this percentage in development? The following provides a ready-made helper class, welcome to take the use, but remember to look at the code a little bit.
/** * Conversion of DP and PX in Android * <p/> * Created by ruicbandroid on 2017/3/26. * * Public class dpandpxhelper { Private Static floatScalePrivate Static float Getscale(Context context) {if(Scale = =0) {Displaymetrics metrics = context.getresources (). Getdisplaymetrics (); scale = metrics.density; }returnScale }/** * px to DP */ Public Static int PX2DP(Context context,intPX) {//+0.5 rounding up return(int) (Px/getscale (context) +0.5f); }/** * DP to PX */ Public Static int dp2px(Context context,intDP) {//+0.5 rounding up return(int) (DP * Getscale (CONTEXT) +0.5f); }}
The above is the whole content of this share, if you feel that it helps, like to express.
If you have any questions, please feel free to contact me or follow my public number to learn more.
Android screen adapted to literacy, tutorials