當(dāng)前位置:首頁(yè) > IT技術(shù) > 移動(dòng)平臺(tái) > 正文

【Android】dip、dp、sp、pt和px的區(qū)別
2021-09-09 14:04:15

?

1、概述

過(guò) 去,程序員通常以像素為單位設(shè)計(jì)計(jì)算機(jī)用戶界面。例如:圖片大小為80×32像素。這樣處理的問(wèn)題在于,如果在一個(gè)每英寸點(diǎn)數(shù)(dpi)更高的新顯示器上 運(yùn)行該程序,則用戶界面會(huì)顯得很小。在有些情況下,用戶界面可能會(huì)小到難以看清內(nèi)容。由此我們采用與分辨率無(wú)關(guān)的度量單位來(lái)開(kāi)發(fā)程序就能夠解決這個(gè)問(wèn)題。 Android應(yīng)用開(kāi)發(fā)支持不同的度量單位。

2、度量單位含義

dip: device independent pixels(設(shè)備獨(dú)立像素). 不同設(shè)備有不同的顯示效果,這個(gè)和設(shè)備硬件有關(guān),一般我們?yōu)榱酥С諻VGA、HVGA和QVGA 推薦使用這個(gè),不依賴(lài)像素。

dp: dip是一樣的

px: pixels(像素). 不同設(shè)備顯示效果相同,一般我們HVGA代表320x480像素,這個(gè)用的比較多。

pt: point,是一個(gè)標(biāo)準(zhǔn)的長(zhǎng)度單位,1pt=1/72英寸,用于印刷業(yè),非常簡(jiǎn)單易用;
sp: scaled pixels(放大像素). 主要用于字體顯示best for textsize。

in(英寸):長(zhǎng)度單位。?
mm(毫米):長(zhǎng)度單位。

3、度量單位的換算公式

在android源碼包TypedValue.java中,我們看如下函數(shù):

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;

}

該函數(shù)功能:是把各單位換算為像素。

metrics.density:默認(rèn)值為DENSITY_DEVICE / (float) DENSITY_DEFAULT;

metrics.scaledDensity:默認(rèn)值為DENSITY_DEVICE / (float) DENSITY_DEFAULT;

metrics.xdpi:默認(rèn)值為DENSITY_DEVICE;

DENSITY_DEVICE:為屏幕密度

DENSITY_DEFAULT:默認(rèn)值為160

?

4、屏幕密度:表示每英寸有多少個(gè)顯示點(diǎn),與分辨率是兩個(gè)不同的概念。

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默認(rèn)的low=120 ;Medium =160; High = 240

5、綜上所述

據(jù)px = dip * density / 160,則當(dāng)屏幕密度為160時(shí),px = dip
根據(jù) google 的建議,TextView 的字號(hào)最好使用 sp 做單位,而且查看TextView的源碼可知Android默認(rèn)使用sp作為字號(hào)單位。將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

開(kāi)通會(huì)員,享受整站包年服務(wù)立即開(kāi)通 >