当前位置:网站首页>21 Days Learning Challenge - Day 1 Punch (Screen Density)

21 Days Learning Challenge - Day 1 Punch (Screen Density)

2022-08-02 10:40:00 Meng Fangfang

1. Recall

I have been engaged in Android development for a few years. Looking back now, I was very happy when I first started to display a few simple controls, then I came into contact with the four major components of Android, then I customized View, and finally I used a third-party open source framework.Do a few functions that I think are cool... and then it seems to stop, and for several years in the middle, I "wasted my life" with this basic thing...

It wasn't until the first two years that I realized that it couldn't go on like this. If I just knew about the ever-changing technology, but I didn't know why, it would soon be eliminated, so I started to study the source code.As soon as I researched the source code, I found that I didn't seem to know anything, and I was in the confusion of just entering the industry...

Take advantage of the 21-day learning challenge opportunity, force yourself to learn hard for 21 days with the great God!

Day 1: Pixel density, screen density, resolution.

2. Background:

In order to adapt to different screen sizes, during the Android development process, you need to add multiple pictures of ldpi, mdpi, nodpi, xhdpi, xxhdpi, xxxhdpi to the resource file.

3. Several concepts

①Screen size

Screen size refers to the physical size of the phone diagonally, in inches (inch), one inch is about 2.54 cm.

General mobile phone sizes are 4 inches, 4.5 inches, 5.0 inches, 5.2 inches, 5.4 inches, 5.99 inches, 6.0 inches, 6.2 inches, etc.

②Screen resolution

Resolution is the total number of pixels on the screen of a mobile phone. Generally, the number of pixels in the screen width is multiplied by the number of pixels in the screen height.The larger the resolution, the more delicate the screen and the more details that can be displayed.

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

Get screen resolution:

int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // screen width

int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // screen height

③Pixel density

Pixel density (dpi, dots per inch; or PPI, pixels per inch) refers to the number of pixels per inch.

④Screen density:

Screen density is actually another representation of pixel density, based on 160dpi=1.0.After the mobile phone leaves the factory, the screen density, including the pixel density in the X and Y axis directions, is a fixed value.

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 2.0.

Android divides the actual screen density into low, medium, high, and extra high, extra extra high.

General screen: ldpi is 120dpi, mdpi is 160dpi, hdpi is 240dpi, xhdpi is 320dpi, xxhdpi is 480dpi.

Get pixel density and screen density:

DisplayMetrics dm = new DisplayMetrics();

dm = getResources().getDisplayMetrics();

float density = dm.density; // screen density (pixel ratio: 0.75/1.0/1.5/2.0)

int densityDPI = dm.densityDpi; // pixel density (pixels per inch: 120/160/240/320)

float xdpi = dm.xdpi; //Pixel density in the X-axis direction

float ydpi = dm.ydpi; //Pixel density in the Y-axis direction

screenWidth = dm.widthPixels; // screen width

screenHeight = dm.heightPixels; // screen height

4. How to load multiple drawables

When the android system adapts to the drawable, it will first look in the dpi directory corresponding to the device.If it is not found, it will follow the principle of "first high then low", and then scale the image proportionally.

For example, it is currently an xhdpi device (there are only xxhdpi, xhdpi, xxhdpi, nodpi, mdpi, hdpi in the project), then the search sequence for drawables is: first search for the xhdpi directory, if not found, then search for xxhdpi, if not found, thenFind xxxhdpi, find nodpi if you haven't found it, find hdpi if you haven't found it, find mdpi if you can't find it, and search in turn.If the target image is found in xxhdpi, it will be compressed by 2/3 for use (because the system thinks it has found an image larger than the appropriate size), if the image is found in mdpi, it will be enlarged by 2 times to use (the system thinks it has found a picture)A picture smaller than the suitable size needs to be enlarged to ensure normal).

原网站

版权声明
本文为[Meng Fangfang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/214/202208021025495200.html