One article to find out Android screen density, pixel density, resolution, Android unit of measurement (dp)

First, let’s clarify a few concepts:

1. Screen resolution (px):

Resolution is the total number of pixels on the screen of a mobile phone. Generally, the number of pixels of the screen width is multiplied by the number of pixels of the screen height. The higher the resolution, the finer the screen and the more details it can display.

Commonly used resolutions are 320x240, 640x480, 1280x720, 1280x960, 1080x1920, 2560x1440, etc., and the unit is pixel. For example, 1080x1920 means that there are 1080 pixels in the screen width direction and 1920 pixels in the screen height direction

2. Pixel density (dpi):

Refers to the number of pixels contained in each inch of the screen

3. Screen density:

It is another representation of pixel density. Android divides the screen based on the pixel density of 160dpi. When the pixel density is 160dpi, the screen density is 1.0, when the pixel density is 120dpi, the screen density is 0.75, and when the pixel density is 320dpi, the screen density is is 2.0, and that is: screen density = pixel density/160

4. Android's unit of measurement, dp, is mainly used to mark the width and height of the control. Its conversion formula:

 The total dp value of the screen = resolution / screen density

5. The length of the screen diagonal, in inches.

2. How to obtain the adb command:

Pixel density (dpi): adb shell wm density

Screen resolution (px): adb shell wm size

3. Code acquisition method:

1. Screen density, pixel density:

DisplayMetrics dm = new DisplayMetrics();

dm = getResources().getDisplayMetrics();

float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)

int densityDPI = dm.densityDpi; // 像素密度(每寸像素:120/160/240/320)

2. Resolution:

Usually, there are two requirements for the obtained screen resolution, one is the actual resolution of the screen, and the other is the actual available resolution of the application due to the occupation of the top or bottom virtual navigation bar (reduced from the actual resolution) occupied part). In the following code example, there are two ways to obtain the requirements:

1. Get the actual screen resolution

method one:

WindowManager windowManager = getWindow().getWindowManager();

Point point = new Point();

windowManager.getDefaultDisplay().getRealSize(point);

//屏幕实际宽度(像素个数)

int width = point.x;

//屏幕实际高度(像素个数)

int height = point.y;

Method Two:

WindowManager windowManager = getWindow().getWindowManager();

DisplayMetrics metrics = new DisplayMetrics();

windowManager.getDefaultDisplay().getRealMetrics(metrics);

//屏幕实际宽度(像素个数)

int width = metrics.widthPixels;

//屏幕实际高度(像素个数)

int height = metrics.heightPixels;

2. Obtain the available resolution of the screen

method one:

WindowManager windowManager = getWindow().getWindowManager();

Display display = windowManager.getDefaultDisplay();

Point point = new Point();

display.getSize(point);

//屏幕可用宽度(像素个数)

int width = point.x;

//屏幕可用高度(像素个数)

int height = point.y;
  1. WindowManager windowManager = getWindow().getWindowManager();

  2. Display display = windowManager.getDefaultDisplay();

  3. Point point = new Point();

  4. display.getSize(point);

  5. //屏幕可用宽度(像素个数)

  6. int width = point.x;

  7. //屏幕可用高度(像素个数)

  8. int height = point.y;

Method Two:

WindowManager windowManager = getWindow().getWindowManager();

Display display = windowManager.getDefaultDisplay();

//屏幕可用宽度(像素个数)

int width = display.getWidth();

//屏幕可用高度(像素个数)

int height = display.getHeight();

3. Android will load corresponding resource files according to the pixel density values ​​of different devices. The drawable folders corresponding to different pixel densities are as follows:

a704face957b48fd9c22da263a9d49b5.png

 4. Taking mdpi as the baseline, the magnification (i.e. scaling factor density) under each density directory is as follows:

eb9ab3b97bbd4ab5bd460896e0c5977c.png

 

 

Guess you like

Origin blog.csdn.net/ahui_123456789/article/details/128639472