当前位置:首页 > IT技术 > 移动平台 > 正文

【Android】dip、dp、sp、pt和px的区别
2021-09-09 14:04:15

 

1、概述

过 去,程序员通常以像素为单位设计计算机用户界面。例如:图片大小为80×32像素。这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上 运行该程序,则用户界面会显得很小。在有些情况下,用户界面可能会小到难以看清内容。由此我们采用与分辨率无关的度量单位来开发程序就能够解决这个问题。 Android应用开发支持不同的度量单位。

2、度量单位含义

dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。

dp: dip是一样的

px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。

pt: point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用;
sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

in(英寸):长度单位。 
mm(毫米):长度单位。

3、度量单位的换算公式

在android源码包TypedValue.java中,我们看如下函数:

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;

}

该函数功能:是把各单位换算为像素。

metrics.density:默认值为DENSITY_DEVICE / (float) DENSITY_DEFAULT;

metrics.scaledDensity:默认值为DENSITY_DEVICE / (float) DENSITY_DEFAULT;

metrics.xdpi:默认值为DENSITY_DEVICE;

DENSITY_DEVICE:为屏幕密度

DENSITY_DEFAULT:默认值为160

 

4、屏幕密度:表示每英寸有多少个显示点,与分辨率是两个不同的概念。

Android主要有以下几种屏:如下表

屏幕

Tyep

宽度

Pixels

高度

Pixels

尺寸

Range(inches)

屏幕密度

QVGA

240

320

2.6-3.0

low

WQVGA

240

400

3.2-3.5

low

FWQVGA

240

432

3.5-3.8

low

HVGA

320

480

3.0-3.5

Medium

WVGA

480

800

3.3-4.0

High

FWVGA

480

854

3.5-4.0

High

WVGA

480

800

4.8-5.5

Medium

FWVGA

480

854

5.0-5.8

Medium

备注

目前android默认的low=120 ;Medium =160; High = 240

5、综上所述

据px = dip * density / 160,则当屏幕密度为160时,px = dip
根据 google 的建议,TextView 的字号最好使用 sp 做单位,而且查看TextView的源码可知Android默认使用sp作为字号单位。将dip作为其他元素的单位。

 

 

 

A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:

dp
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
sp
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
pt
Points - 1/72 of an inch based on the physical size of the screen.
px
Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
mm
Millimeters - Based on the physical size of the screen.
in
Inches - Based on the physical size of the screen.


 

本文摘自 :https://blog.51cto.com/u

开通会员,享受整站包年服务立即开通 >