preface

First of all, Android fragmentation is a serious problem. Because There are so many Android vendors and so many devices, there are so many different resolutions. To address fragmentation, new units such as DP and SP are defined in the Android development documentation

Relevant concepts

  • Different devices have different display effects. This depends on the device hardware. Generally, we recommend using this to support WVGA, HVGA, and QVGA, regardless of pixels.

    Note that the DIP is related to screen density, and screen density is related to specific hardware. Improper hardware Settings may cause the DIP to fail to display properly. On a screen density of 160, 1dip=1px. Sometimes you may have a large screen resolution of 480*800, but the screen density is not set correctly, for example, 160.

    Dip (value) =(int) (px (value) /1.5 + 0.5)

  • Dp: It’s very simple. It’s the same as DIP.

  • Px :px is the smallest unit for a long picture. Different devices and different screens display the same pixels. This is the absolute number of pixels. It will always be the same.

  • Sp: Scaled Pixels. Mainly used for font display best for textsize.

Learn more about: https://www.cnblogs.com/wangjiafang/p/4433912.html

Common mobile phone screen pixels and corresponding differentiation rate levels:

Degree of separateness The density of
ldpi 320 * 240
mdpi 480 * 320
hdpi 800 * 480
xhdpi 1280 * 720
xxhdpi 1920 * 1080

Simple conversion between dp and PX:

Degree of separateness Conversion relation
Ldpi mobile phone 1 dp = 0.75 px
Mdpi mobile phone 1 dp = 1.0 px
Hdpi mobile phone 1 dp = 1.5 px
Xhdpi mobile phone 1 dp = 2.0 px
Xxhdpi mobile phone 1 dp = 3.0 px

Based on the above description, we came to the following conclusion: for MDPI phones, our layout can be adapted by dp units

Here’s an example:

Assume that the physical Size of a Screen with QVGA (320×480) resolution is 2 inches *1.5 inches, then the dpi of this phone =160 Screen Size refers to the actual physical Size of the phone, such as iPhone4S is 3.5 inches. By 3.5 inches, I mean that the screen is 3.5 inches diagonally.

Note: Dip is used for pixels and SP is used for fonts. For example, textSize=”16sp”, layout_width=”60dp”; Occasionally you need to use px units, such as when you need to draw a thin dividing line on the screen. This is also recommended by Google.

conversion

public static int dip2px(Context context, float dipValue){ 
                final float scale = context.getResources().getDisplayMetrics().density; 
                return (int)(dipValue * scale + 0.5f); 
        } 
        
    public static int px2dip(Context context, float pxValue){ 
                final float scale = context.getResources().getDisplayMetrics().density; 
                return(int)(pxValue/scale + 0.5f); }Copy the code

Pixel transform

When we write a layout, we still need to know how many px are in a DP.

Dp = (DPI/ (160 pixels/inch)) px = density px

Notice, it’s all units here. Px is the unit, DP is the unit, density is not the unit.

For convenience, if the DPI is 240 pixels/inch, the density is 1.5

So dp=1.5px, note that this is in units, i.e. device-independent pixels = density pixels

So in numerical terms, it would be this

PX = density * DP

That is, pixel value = density * device independent pixel value, please note that there is a value word here.

Why the standard DPI = 160

(1) In Android Design [1], the DPI of mainstream devices is divided into four levels: 120 DPI, 160 DPI, 240 DPI and 320 DPI

In practical development, we often need to convert these dimensions to each other (for example, design is completed at a certain resolution, and then output after scaling to other sizes). Generally, the dimensions of elements in the interface are defined in accordance with the ratio between DPI, i.e. 2:1.5:1:0.75.

That is to say, if 160 dpi is used as the benchmark, as long as the dimension DP is a common multiple of 4, XHDPI times 2, HDPI times 1.5, LDPI times 0.75 can satisfy the integer pixel under all sizes.

But assuming 240 dpi as standard, that requires DP to be a common multiple of 3, XHDPI multiplied by 1.333, MDPI multiplied by 0.666, LDPI divided by 2

Using LDPI and XHDPI as benchmarks is more complicated, so 160 DPI is chosen

(2) This is explained in Google's official documentation, because the first Android device (HTC's T-Mobile G1) was 160DPI.Copy the code

Why is it better to use DIP instead of PX in layout?

Because there are a lot of different screen densities in the world. What is screen density? It’s dPI, which is the number of pixels per unit length.

Imagine if these phones were the same size and the screen densities varied widely. Would one phone have very few pixels in the horizontal direction and the other one a lot of pixels in the horizontal direction? So when we draw the same number of Pix’s, it shows

Wouldn’t the length be different?

For example, for the two mobile phones in the picture below, a Button with a length of 2px will appear smaller in the mobile phone with a higher screen density.

The Button with the length of 2DIP set at the same time has the same size displayed on the two phones.

For more

An introduction to screen Fit Sizes

Crack Android version of wechat jump jump, a trick to teach you challenge high scores

Remember a ali interview experience | Java knowledge to sort out the interview

NDK project actual combat – high imitation 360 mobile phone assistant uninstall monitoring

Believe in yourself, there is nothing impossible, only unexpected

Wechat official account: Terminal R&D Department