Details screen size acquisition and conversion of dp and px values in Android applications

  • 2021-06-28 14:00:04
  • OfStack

Get screen size

Obtained from WindowManager


DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

// among display = getWindowManager().getDefaultDisplay()  Obtained 1 individual DefaultDisplay object ;  Then?  display.getMetrics(dm)  Assign screen size information to DisplayMetrics dm

// Be careful: WindowManager Sometimes you need to pass context . getSystemService Obtain: WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);

The relationship between DisplayMetrics and Display.

Display refers to the display area, which may be a real physical screen, or only the display area of an application, such as in non-full-screen Activity, where the system has a status bar so the display area is smaller than the physical screen.DisplayMetrics encapsulates various attribute values for the display area.Looking at the source code, it was found that the DisplayMetrics comment on each attribute value indicated its true physical size.It is also found that the function display.getMetrics (dm) is basically used to obtain the real screen size.Remember this one point.

Note: The constructor DisplayMetrics does not need to pass any parameters;After calling getWindowManager (), you get the Handle of the existing Activity.diplay then stores the resulting width and height dimensions in the DisplayMetrics object, which are in pixels (Pixel). Pixels refer to absolute pixels, not relative pixels.

The following information is available through the DisplayMetrics object dm


width = dm.widthPixels;

height = dm.heightPixels;

xdpi = dm.xdpi;

ydpi = dm.ydpi;

density = dm.densityDpi;

fdensity = dm.density;

Examples of converting dp and px to corresponding px values:


int padding =4 ; 

padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4,

context.getResources().getDisplayMetrics());


Explanation:
The units of padding are: dip, the size of padding is: 4

Although it is 4dip, it is not the real unit of the final padding.

After calculation, he multiplied 4dp by the density factor to get a value. In fact, the calculated padding units are pixels, that is, its width.On different dpi screens, this value is different.

That is, converting dp to px returns an px value corresponding to dp.

If this is COMPLEX_UNIX_SP means that sp is converted into dp.

Conversion between units uses this method, which can be encapsulated into a tool method.

Step 1 Understanding:
The applyDimension method converts 4 pixels to 6dp (480x800), 4dp (320x800), and 3dp (240x320), so the returned values correspond to different resolutions (obtained through getDisplayMetrics) and are 6, 4, 3

Source code:


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;

}


Related articles: