-
definition -
Px is a unit of relative length, which represents the physical pixels on a display device. -
Each px corresponds to a small square on the screen, which together form the image we see.
-
-
characteristic -
Px is an absolute unit and does not change with screen resolution or scaling. -
The px size of different devices may be different because their physical pixel density (DPI) is different.
-
-
In development, Px is often used to set the size, location and other attributes of elements.
-
On different devices, the same px value may render different actual sizes, because the device DPI is different.
-
Sp (scale independent pixels) is a relative length unit, mainly used to set font size. -
Similar to dp, it also scales according to the user's font size settings. -
Normally, The conversion ratio of sp and dp is 1:1, but if the user changes the system font size setting, The sp will change accordingly, while the dp will not.
-
Compared with px, Sp can ensure that the size of text displayed on different devices is consistent. -
This is because sp will scale according to the user's font size settings, while px will not. -
For example, even on devices with different resolutions or densities, the font size set with sp will look the same.
-
In XML layout files, you can use sp units to set the size of text: < TextView android: layout_width = " wrap_content " android: layout_height = " wrap_content " android: textSize = " 16sp " />
-
Dp is a relative length unit independent of screen density, while sp is a relative length unit independent of user font size settings. -
In most cases, The conversion ratio of sp and dp is 1:1, but if the user changes the system font size setting, The sp will change accordingly, while the dp will not. -
Therefore, for setting the text size, we should give priority to using sp instead of px or dp to ensure that the size of text displayed on different devices is consistent.
-
Dp (identity dependent pixels) and dip (device dependent pixels) are the same concept, which refers to a relative length unit. -
It is independent of the screen density of the device, which can ensure that under different resolutions and screen densities, The physical dimensions of UI elements are consistent. -
1 dp equals 1/160 inch, which means that on a 160 dpi screen, 1 dp equals 1 px.
-
On different devices, the same px value may render different actual sizes, because the device DPI is different. -
Using dp/dip can solve this problem, because it will automatically scale according to the screen density of the device to ensure that the UI elements maintain the same physical size on different devices. -
This is very important for developers, because it can ensure that applications can be displayed and run normally on various devices.
-
In the XML layout file, you can use dp units to set the size of UI elements: < TextView android: layout_width = " 200dp " android: layout_height = " 50dp " android: textSize = " 16sp " />
-
1 inch=2.54 cm -
1 cm=0.393701 inch
public class UnitConverter { public static double cmToInch(double cm) { return cm * 0.393701; } public static double inchToCm(double inch) { return inch * 2.54; } } //Example of use double centimeters = 10.0; double inches = UnitConverter.cmToInch(centimeters); System.out.println(centimeters + " cm = " + inches + " inches"); double inchesValue = 5.0; double centimetersValue = UnitConverter.inchToCm(inchesValue); System.out.println(inchesValue + " inches = " + centimetersValue + " cm");
10.0 cm = 3.93701 inches 5.0 inches = 12.7 cm
DisplayMetrics displayMetrics = new DisplayMetrics ( ) ;
getWindowManager ( ) . getDefaultDisplay ( ) . getMetrics ( displayMetrics ) ;
int width = displayMetrics . widthPixels ;
int height = displayMetrics . heightPixels ;
WindowManager windowManager = ( WindowManager ) getSystemService ( Context . WINDOW_SERVICE ) ;
Display display = windowManager . getDefaultDisplay ( ) ;
Point size = new Point ( ) ; display . getSize ( size ) ;
int width = size . x ;
int height = size . y ;
Resources resources = getResources ( ) ;
DisplayMetrics metrics = resources . getDisplayMetrics ( ) ;
int width = metrics . widthPixels ;
int height = metrics . heightPixels ;
Configuration configuration = getResources ( ) . getConfiguration ( ) ;
int widthDp = configuration . screenWidthDp ;
int heightDp = configuration . screenHeightDp ;
-
Standard definition (SD) resolution: 640x480 -
High definition (HD) resolution: 1280x720 -
Full HD resolution: 1920x1080 -
2K resolution: 2048x1080 -
4K resolution: 3840x2160
-
Number of pixels -
Image resolution refers to the number of pixels contained in the image, usually expressed in the form of width x height, such as 1920x1080, 3840x2160, etc. -
The more pixels, the richer the details of the image and the clearer the picture.
-
-
Pixel density -
Pixel density, also known as PPI (Pixels Per Inch) or DPI (Dots Per Inch), represents the number of pixels per inch. -
The higher the pixel density, the clearer and more delicate the image looks under the same physical size.
-
-
Color depth -
Color depth describes the number of colors that can be represented by each pixel in the image. -
The higher the color depth, the stronger the color expression of the image. Common color depths are 8 bits (256 colors), 16 bits (65536 colors), 24 bits (16.7 million colors), etc.
-
-
Display quality -
A high DPI value means that the details displayed are richer and clearer in the same physical space. -
Under the same physical size, the high DPI display screen can provide better display effect.
-
-
Print quality -
The details and sharpness of the printed output are closely related to the DPI value. -
Generally, the higher the DPI value of the printer, the clearer the printed image.
-
-
Image quality -
The DPI value of digital image determines the final resolution and detail performance of the image. -
At the same physical size, Images with high DPI values can provide better display and printing effects.
-
-
72 DPI: default DPI value of Windows operating system, used for screen display. -
96 DPI: the default DPI value of Windows XP and later versions, used for screen display. -
150-300 DPI: The DPI value range of general printouts can provide high print quality. -
300-600 DPI: The DPI value range of high-quality printouts can achieve very clear results. -
1200-2400 DPI: DPI value range of industrial grade printer, used for high-precision commercial printing.
-
Question: What does √ (width pixels ^ 2+height pixels ^ 2) mean?
-
Example of PPI calculation Suppose we need to calculate the dpi (pixel density) of the MacBook Pro 13 inch model
var ppi = Math.sqrt(2560 * 2560 + 1600 * 1600) / 13.3; console.log(ppi); //≈ two hundred and twenty-seven
| | |
---|---|---|
| | |
| | |
| | |
| | |
| | |
| | |
-
Resolution determines the total number of pixels of the entire screen or image, regardless of screen size. -
PPI determines the pixel density per inch on the screen at the same resolution. PPI=resolution/screen size (inches) -
DPI and PPI are equivalent in digital display equipment. But in the field of printing, DPI describes the resolution of the printer, not the resolution of the display device.
-
Pixel is the smallest display unit on the screen. -
Px is the actual number of pixels on the screen and is an absolute length unit. -
It is independent of the screen density of the device, so the same px value will occupy different physical dimensions on devices with different resolutions.
-
Dp is a relative unit of length, independent of screen density. -
1 dp is equal to 1/160 inch. Under different screen densities, the same dp value will occupy the same physical size. -
This is the most commonly used length unit in Android development, because it can adapt to different screen densities.
-
Sp is also a relative length unit, which is used to set font size. -
Similar to dp, it also scales according to the user's font size settings. -
Normally, The conversion ratio of sp and dp is 1:1, but if the user changes the system font size setting, The sp will change accordingly, while the dp will not.
import android . content . Context ;
import android . util . DisplayMetrics ;
import android . util . TypedValue ;
public class UnitConverter {
public static int dpToPx ( Context context, float dp ) {
DisplayMetrics displayMetrics = context . getResources ( ) . getDisplayMetrics ( ) ;
return Math . round ( dp * displayMetrics . density ) ;
}
public static int pxToDp ( Context context, float px ) {
DisplayMetrics displayMetrics = context . getResources ( ) . getDisplayMetrics ( ) ;
return Math . round ( px / displayMetrics . density ) ;
}
public static int spToPx ( Context context, float sp ) {
return Math . round ( TypedValue . applyDimension ( TypedValue . COMPLEX_UNIT_SP , sp, context . getResources ( ) . getDisplayMetrics ( ) ) ) ;
}
public static int pxToSp ( Context context, float px ) {
return Math . round ( px / context . getResources ( ) . getDisplayMetrics ( ) . scaledDensity ) ;
}
}
//Convert 20dp to px
int pixels = UnitConverter . dpToPx ( this , 20f ) ;
//Convert 100px to dp
int dp = UnitConverter . pxToDp ( this , 100f ) ;
//Convert 16sp to px
int fontPixels = UnitConverter . spToPx ( this , 16f ) ;
//Convert 32px to sp
int scaledPixels = UnitConverter . pxToSp ( this , 32f ) ;