In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of what the length unit in the Android layout is, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Android layout of what the length unit is, let's take a look.
1. Let's take a look at which units of length are supported by Android:
Px: pixels (pixels). The display effect of different devices is the same. For example, our screen width of 800mm 480 is 800px.
Dip: device independent pixels (device independent pixel). Different devices have different display effects, which is related to the device hardware. Usually, when the screen is large, the density is big, and when the screen is small, the density is small.
When the actual screen resolution is 240px*400px, densityDpi=120
The actual resolution of the screen is 320px533pxdensityDpirated 160
The actual resolution of the screen is 480px800 pxDpirates 240.
The conversion relationship between dip and px is:
Pixs = dips * (densityDpi/160), that is, when densityDpi=160, 1dip=1px
Sp: scaled pixels (magnified pixels). The size of the sp depends on the system metrics.scaledDensity value.
Pt: point, a standard unit of length, 1pt=1/72 inches, used in the printing industry (basically not needed)
The conversion relationship between pt and px: pixs = pt*xdpi * (1.0f/72); xdpi represents the number of pixels per inch
In (inch) unit of length (hardly needed)
Conversion relationship between in and px: pixs = in*xdpi
Mm (millimeter) unit of length (rarely needed)
The conversion relationship between mm and px: pixs = mm * xdpi * (1.0f/25.4f)
two。 The system acquires the unit of length
If you look at the meaning of the specific length units above, you will have a question. The conversion of different units depends on some attributes of the system, such as the value of densityDpi and the value of xdpi. Where did the system get these values? just look at the test case:
Public void testgetdisplay () {WindowManager wm = (WindowManager) this.getInstrumentation (). GetContext (). GetSystemService (Context.WINDOW_SERVICE); DisplayMetrics mDisplayMetrics = new DisplayMetrics (); wm.getDefaultDisplay (). GetMetrics (mDisplayMetrics); System.out.println ("display.height=" + wm.getDefaultDisplay (). GetHeight ()); System.out.println ("display.width=" + wm.getDefaultDisplay (). GetWidth ()); System.out.println ("densityDpi=" + mDisplayMetrics.densityDpi); System.out.println ("xdpi=" + mDisplayMetrics.xdpi) System.out.println ("density=" + mDisplayMetrics.density);}
The relationship between 3.densityDpi and drawable- (hdpi,mdpi,ldpi)
The system drawable has three folders of hdpi,mdpi,ldpi to store pictures of different sizes. The file under which file is used is related to the system densityDpi value.
DensityDpi=120:ldpidensityDpi=160:mdpidensityDpi=240:hdpi
As I said earlier, densityDpi depends on the display screen, so you can see why different display WVGA,HVGA,QVGA use different drawable- (hdpi,mdpi,ldpi) images.
The resolution is 240pxmemory 400pxcentury densityDpirated 120mm-> QVGA:ldpi
The resolution is 320px533px density Dpirates 160-- > HVGA:mdpi
The resolution is 480pxmemory 800px.densityDpirated 240-> WVGA:WVGA
4. In-depth understanding of the code
Although I understand the above theoretical values, sometimes I find that different units of length are set up, but the effect shown is unexpected. I have encountered this problem of scratching my head before. In order to solve this problem, I have to go deep into the code and find out.
Before going into the code, we need to be clear about one problem, that is, all the length values in the code are px. There is no ready-made example on hand. Take the / Launcher2/res/layout-land/workspace_screen.xml I am studying as an example to see a custom attribute:
Launcher:cellWidth= "105pt"
This property customizes the width of a desktop shortcut icon. If the reader tests it himself, write a test view and set the property:
Android:layout_width= "800px"
It's the same.
When view is created, the attribute values in xml are stored in the parameter AttributeSet attrs:
Public CellLayout (Context context, AttributeSet attrs, int defStyle)
Continue to look at the implementation code of the constructor:
Public CellLayout (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); / / get all the custom attributes in the custom attribute group CellLayout. With regard to custom attributes, there is no expansion description TypedArray a = context.obtainStyledAttributes (attrs, R.styleable.CellLayout, defStyle, 0); / / get the value of the attribute cellWidth, and the length unit will be converted to px mCellWidth = a.getDimensionPixelSize (R.styleable.CellLayout_cellWidth, 10). }
The key code to achieve the conversion of length units is in a.getDimensionPixelSize (R.styleable.CellLayout_cellWidth, 10), which goes directly to the key code:
Public int getDimensionPixelSize (int index, int defValue) public static int complexToDimensionPixelSize (int data,DisplayMetrics metrics) public static float applyDimension (int unit, float value,DisplayMetrics metrics) {switch (unit) {case COMPLEX_UNIT_PX: return value; case COMPLEX_UNIT_DIP: return value * metrics.density; case COMPLEX_UNIT_SP: return value * metrics.scaledDensity; case COMPLEX_UNIT_PT: return value * metrics.xdpi * (1.0f/72); case COMPLEX_UNIT_IN: return value * metrics.xdpi Case COMPLEX_UNIT_MM: return value * metrics.xdpi * (1.0f/25.4f);} return 0;} this is the end of the article on "what is the unit of length in the Android layout". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the length unit in the Android layout". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.