I just learned this recently. Let me briefly explain my understanding. If there are any mistakes, please correct me
Regarding the conversion of dp and px, take a look at their conversion formula
dpi = Number of pixels in the horizontal (vertical) direction of the screen/Horizontal (vertical) size of the screen dp = (px * 160) / (Number of pixels in the horizontal (vertical) direction of the screen/Horizontal (vertical) size of the screen size)
Can you say that screen size and resolution have nothing to do with each other? Their influence is not direct, but indirect.
It’s okay if you understand this way. DP was born for adaptation. In the past, you set the button width to 20px in the layout file. When debugging the application on mobile phones with different resolutions, you will find that they are different sizes, so you have to manually adapt, which is very tiring. If the width is 20dp, the system will display the same size at different resolutions after internal conversion. This is why they are the same size when you use a ruler to measure them.
The last question, I won’t answer it because I have never done similar work, but you can refer to the official guidance document on TV development
Simply put, dpi (dots-per-inch) refers to the number of dots per inch, not the pixels per inch. PPI (pixel-per-inch) refers to the pixels per inch. Calculating the screen size is based on ppi The calculation is not by dpi. The conversion formula between dp and px is: px = dp * density density value refers to the logical density of the screen. How to calculate this logical density? Generally, the density value of 160dpi is 1. For example, the density of 1920*1080 is generally 12X9. In actual use, you also need to consider the space occupied by the status bar and title bar of the mobile phone. Assuming it is 20dp size, its pixels on 320480 are 20 2 = 40px, and on 19201080 it is 20 12 = 240px. How can it be the same size. After getting the pixel px, the size of the control is finally determined based on the ppi in the x and y directions of the screen. The values mentioned above can be obtained through DisplayMetrics, density = mContext.getResources().getDisplayMetrics().density ppi in the x-axis direction of the screen = mContext.getResources().getDisplayMetrics().xdpi ppi in the y-axis direction of the screen = mContext.getResources().getDisplayMetrics().ydpi
Be reasonable. . Use the 100% layout library directly. . . dp This is a deceptive setting. . . How many technical guys and design girls have been beaten to death on this DP. . .
I just learned this recently. Let me briefly explain my understanding. If there are any mistakes, please correct me
Regarding the conversion of dp and px, take a look at their conversion formula
dpi = Number of pixels in the horizontal (vertical) direction of the screen/Horizontal (vertical) size of the screen
dp = (px * 160) / (Number of pixels in the horizontal (vertical) direction of the screen/Horizontal (vertical) size of the screen size)
Can you say that screen size and resolution have nothing to do with each other? Their influence is not direct, but indirect.
It’s okay if you understand this way. DP was born for adaptation. In the past, you set the button width to 20px in the layout file. When debugging the application on mobile phones with different resolutions, you will find that they are different sizes, so you have to manually adapt, which is very tiring. If the width is 20dp, the system will display the same size at different resolutions after internal conversion. This is why they are the same size when you use a ruler to measure them.
The last question, I won’t answer it because I have never done similar work, but you can refer to the official guidance document on TV development
Simply put, dpi (dots-per-inch) refers to the number of dots per inch, not the pixels per inch. PPI (pixel-per-inch) refers to the pixels per inch. Calculating the screen size is based on ppi The calculation is not by dpi. The conversion formula between
dp and px is:
px = dp * density
density value refers to the logical density of the screen. How to calculate this logical density? Generally, the density value of 160dpi is 1. For example, the density of 1920*1080 is generally 12X9. In actual use, you also need to consider the space occupied by the status bar and title bar of the mobile phone.
Assuming it is 20dp size, its pixels on 320480 are 20 2 = 40px, and on 19201080 it is 20 12 = 240px. How can it be the same size.
After getting the pixel px, the size of the control is finally determined based on the ppi in the x and y directions of the screen.
The values mentioned above can be obtained through DisplayMetrics,
density = mContext.getResources().getDisplayMetrics().density
ppi in the x-axis direction of the screen = mContext.getResources().getDisplayMetrics().xdpi
ppi in the y-axis direction of the screen = mContext.getResources().getDisplayMetrics().ydpi
Be reasonable. . Use the 100% layout library directly. . . dp This is a deceptive setting. . . How many technical guys and design girls have been beaten to death on this DP. . .