If you want to adapt to the same application on different phone models, if you do not use px for all dp in xml, there is still a possibility of adaptation problems!
There are countless mistakes and I think I am using dp. Why does the proportion on mobile phone A look different from that on mobile phone B, why is it displayed on mobile phone A while on mobile phone B on the screen?
Every explanation is very tiring, so I wrote this blog
First, clarify several concepts
The density value indicates the number of display points per inch (*)
Dip/dp: device independent pixels (device independent pixel)
Note: dip is related to screen density, screen density is related to hardware, and the hardware settings are incorrect, which may cause the dip to fail to be displayed normally. On a display with a screen density of 160, 1dip = 1px
Below are some resolution information
Name resolution screen Density
QVGA 320*240 120
WQVGA400 400*240 120
WQVGA432 432*240 120
HVGA 640*480 160
WSVGA 1024*600 160
WXGA800 1280*800 160
WVGA800 800*480 240
WVGA854 854*480 240
WXGA720 1280*720 320
Unless otherwise specified, the screen width refers to the number of screens.
We put an image on mobile phone A, which is px wide and px wide. That is to say, the image width accounts for half of the screen width.
If the application is installed on mobile phone B and the width of B is PX, what is the image width we want? If you want to scale proportionally, the image width should be 50% PX, accounting for of the screen width.
If the unit we use in xml is dp, let's see how to maintain this ratio:
Px = (density/160) dp (density here is)
The expected ratio is recorded as rate, baseDensity = 160, the screen width (number of screens) is x, and the screen density is density.
Then rate = (density/baseDensity) * dp)/x;
Here, baseDensity is known as = 160, and dp is also known, because you wrote it.
Density screen density and screen width x
Rate can be written as follows:
Rate = (dp/baseDensity) * (density/x );
Now the situation is clear, rate = K (constant) * (density/x );
If you want to keep the rate unchanged, you must ensure the density/x ratio.
Explain two more sentences to students with poor mathematics
To maintain the rate, you must set the screen density of mobile phone A to the screen width of mobile phone B.
Or the screen density of mobile phone A/mobile phone B = Mobile Phone A's screen width/mobile phone B's screen width
Similarly, if you want to maintain vertical and proportional scaling, you also need to maintain the ratio.
Only in this way can your application be scaled proportionally.
Using dp to maintain the ratio is only related to this, and has nothing to do with your screen size.
Another way to maintain the ratio is to use the proportional method to define the component size.
However, it is very limited. Only the android: layout_weight attribute can be used in LinearLayout.
It is easy to understand. Here is an example.
Many people think that if dp is used in all projects, it can be transplanted perfectly.
One of our porting projects is to migrate an application from A (resolution: WXGA720 = 1280*720) to B (resolution: WVGA800 = 800*480)
The density of A is 320, and that of B is 240.
Let's take A look at the number of dp IDs in A horizontal direction.
A dp COUNT = 720/(320/160) = 360
B dp COUNT = 480/(240/160) = 320
Mobile phone A has 360 dp at the horizontal direction. If your picture occupies 360 dp, where can B find your 40dp! It must be displayed outside the screen!
Ps: Below is a bit of related content
The following is the void android. util. DisplayMetrics. setToDefaults () function ()
[Java]
Public void setToDefaults (){
WidthPixels = 0;
HeightPixels = 0;
Density = DENSITY_DEVICE/(float) DENSITY_DEFAULT;
DensityDpi = DENSITY_DEVICE;
ScaledDensity = density;
Xdpi = DENSITY_DEVICE;
Ydpi = DENSITY_DEVICE;
NoncompatWidthPixels = 0;
NoncompatHeightPixels = 0;
}
The comment on the density variable is as follows:
Comment the density variable
Float android. util. DisplayMetrics. density
The logical density of the display.
This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5 "x 2" screen ), providing the baseline of the system's display.
Thus on a 160 dpi screen this density value will be 1; on a 120 dpi screen it wocould be. 75; etc.
This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi.
For example, a 240x320 screen will have a density of 1 even if its width is 1.8 ", 1.3", etc.
However, if the screen resolution is increased to 320x480 but the screen size remained 1.5 "x 2" then the density wocould be increased (probably to 1.5 ).
See Also:
DENSITY_DEFAULT
It can be seen that this density is an approximate value and is not strictly calculated based on the actual screen size.
Author: su1216