[android] “px”,“dip”,“dp”和“sp”有什麼區別?


Answers

幾乎所有關於此的內容以及如何為不同大小和密度的多屏幕獲得最佳支持在此處都有很好的記錄:

屏幕尺寸
實際的物理尺寸,按屏幕的對角線測量。 為了簡單起見,Android將所有實際屏幕尺寸分為四種廣義尺寸:小尺寸,普通尺寸,大尺寸和超大尺寸。

屏幕密度
屏幕物理區域內的像素數量; 通常稱為dpi(每英寸點數)。 例如,與“正常”或“高”密度屏幕相比,“低”密度屏幕在給定物理區域內具有更少的像素。 為了簡單起見,Android將所有實際的屏幕密度分為六個一般密度:低,中,高,超高,超高和超高超。

方向
從用戶的角度來看屏幕的方向。 這是橫向或縱向,這意味著屏幕的寬高比分別為寬或高。 請注意,不同的設備默認情況下在不同的方向上運行,但是當用戶旋轉設備時,方向會在運行時更改。

解析度
屏幕上物理像素的總數。 當添加對多個屏幕的支持時,應用程序不會直接使用解決方案; 應用程序應僅關注屏幕大小和密度,如廣義大小和密度組所指定。

與濃度無關的像素(dp)
定義用戶界面佈局時應使用的虛擬像素單位,以密度無關的方式表示佈局尺寸或位置。 與密度無關的像素相當於160dpi屏幕上的一個物理像素,這是系統為“中等”密度屏幕假設的基準密度。 在運行時,系統根據所使用的屏幕的實際密度,根據需要透明地處理dp單位的任何縮放比例。 將dp單位轉換為屏幕像素很簡單: px = dp * (dpi / 160) 。 例如,在240 dpi屏幕上,1 dp等於1.5個物理像素。 定義應用程序的用戶界面時,應始終使用dp單位,以確保在不同密度的屏幕上正確顯示您的用戶界面。

如果您對開發適用於多種類型設備的Android應用程序非常認真,那麼您應該至少閱讀一次屏幕支持開發文檔。 除此之外,了解具有特定屏幕配置的活動設備的實際數量總是一件好事。

Question

android度量單位有什麼區別?

  • PX
  • DP
  • SP



Before answering this question let me decrease the number of units first. So here you go: dp or dip are both the same and are known as Density-independent pixels .

1. px - stands for pixels. Pixels are a single dot, point on a screen. Generally in the mobile industry it is measured in ppi (pixels per inch). Screen resolution is directly proportional to ppi, the larger the number of pixels per inch the higher the screen resolution.

For example, if you draw an image of a size 200 px * 200 px , then its appearance must be different on a high-resolution device versus a low-resolution device. The reason is a 200 px image on a low-resolution phone will be look larger than on a high-resolution device.

Below images are showing a resolution of the same image on different phones -

  • Phone with High screen resolution

  • Phone with Low screen resolution

2. dip or dp - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. "Density independence" refers to the uniform display of UI elements on screens with different densities.

  • Image which is showing 80px (left side image) and 80 dp (right-side image) . Checkout difference.

A dp is equal to one physical pixel on a screen with a density of 160 . To calculate dp:

dp = (width in pixels * 160) / screen density

3. sp - stands for scalable pixels. Generally sp is used for texts on the UI, and sp preserves the font settings. For example, if a user selected a larger font than 30 sp it will auto scale to appear large according to a user preference.




基本上,px適用的唯一時間是一個px,這就是說,如果你想在屏幕上精確地顯示一個像素,就像分割器一樣:

在> 160 dpi時,您可能會獲得2-3像素,

在> 120dpi時,它舍入為0。




像素 - 每個比例點對應屏幕上的實際像素。

以英寸為單位 - 基於屏幕的物理尺寸。

mm毫米 - 基於屏幕的物理尺寸。

點數 - 基於屏幕物理尺寸的1/72英寸。

dp密度 - 獨立像素 - 基於屏幕物理密度的抽象單位。 這些單位相對於160 dpi屏幕,所以一個dp是160 dpi屏幕上的一個像素。 dp與像素的比率將隨屏幕密度而變化,但不一定成正比。 注意:編譯器接受dipdp ,但dpsp更加一致。

sp -Scale獨立的像素 - 這就像dp單元一樣,但它也會根據用戶的字體大小首選項進行縮放。 建議您在指定字體大小時使用本機,以便根據屏幕密度和用戶偏好進行調整。

以兩個屏幕尺寸相同但屏幕密度為160 dpi(每英寸點數,即像素每英寸)為例,另一個為240 dpi。

                          Lower resolution   screen          Higher resolution, same size
Physical Width                      1.5 inches                        1.5 inches
Dots Per Inch (“dpi”)               160                               240
Pixels (=width*dpi)                 240                               360
Density (factor of baseline 160)    1.0                               1.5

Density-independent Pixels          240                               240
(“dip” or “dp” or “dps”)

Scale-independent pixels 
 (“sip” or “sp”)                  Depends on user font size settings    same



在哪裡可以使用px和dp之間的關係?

與濃度無關的像素(dp)

定義用戶界面佈局時應使用的虛擬像素單位,以密度無關的方式表示佈局尺寸或位置。 如上所述,與密度無關的像素相當於160dpi屏幕上的一個物理像素,這是系統為“中等”密度屏幕假設的基準密度。 在運行時,系統根據所使用的屏幕的實際密度,根據需要透明地處理dp單位的任何縮放比例。 將dp單位轉換為屏幕像素很簡單:

px = dp *(dpi / 160)。

例如,在240 dpi屏幕上,1 dp等於1.5個物理像素。 定義應用程序的用戶界面時,應始終使用dp單位,以確保在不同密度的屏幕上正確顯示您的用戶界面。

將像素理解為dp,反之亦然是非常必要的(尤其對於向創意團隊提供精確的dp值)

dp = px * 160 / dpi

MDPI = 160 dpi || Therefore, on MDPI 1 px = 1 dp
For example, if you want to convert 20 pixel to dp, use the above formula,
dp = 20 * 160 / 160 = 20.
So, 20 pixel = 20 dp.

HDPI = 240 dpi - So, on HDPI 1.5 px = 1 dp
XHDPI = 320 dpi - So, on XHDPI 2 px = 1 dp
XXHDPI = 480 dpi - So, on XXHDPI 3 px = 1 dp

For example, let us consider Nexus 4.
If 24 pixels to be converted to dp and if it is a Nexus 4 screen, developers can
convert it to dp easily by the following calculation :
dp = 24 * 160 / 320 = 12 dp
Screen dimension:
768 x 1280 pixel resolution (320 ppi or 320dpi)
Optional (screen size):
 4.7" diagonal
  • 嘗試從廣告素材小組獲取偶數數量的所有像素值。 否則在乘以0.5時會發生精度損失。

PX

上面已經解釋過了。 盡量避免在佈局文件中。 但有些情況下,需要px。 例如,ListView分隔線。 px在這裡更好,因為在整個屏幕分辨率下給出一個像素線作為分頻器。

SP

使用sp來表示字體大小。 然後,只有設備字體大小發生變化時,應用程序內的字體才會發生變化(即顯示 - >設備上的字體)。 如果你想在應用中保留一個靜態大小的字體,你可以在dp中給字體維度。 在這種情況下,它永遠不會改變。 開發人員可能會對某些特定的屏幕有這樣的要求,因此開發人員可以使用dp而不是sp。 在所有其他情況下,建議使用sp。




dpi -

  • 每英寸點數
  • 測量屏幕的像素密度。

像素 - 像素

  • 用於映射屏幕像素

點 - 點

  • 約1/72英寸,相對於物理屏幕尺寸。

英寸 - 相對於物理屏幕尺寸(1英寸= 2.54厘米)。

毫米 - 相對於物理屏幕尺寸。

與尺度無關的像素。

  • 基於用戶的字體大小偏好。
  • 字體應該在'sp'中。

浸 -

  • dip == dp
  • 密度獨立像素。
  • 它根據屏幕密度而變化。
  • 在160 dpi的屏幕上,1 dp = 1像素。
  • 使用除文本字體大小以外的dp。

在標準中,使用了dp和sp。 sp代表字體大小,dp代表其他所有內容。

單位換算公式:

px = dp *(dpi / 160);

Density Bucket -> Screen Display => Physical Size        => Pixel Size                   

ldpi         -> 120 dpi          => 0.5 x 0.5 in         => 0.5 in * 120 dpi = 60x60 px   

mdpi         -> 160 dpi          => 0.5 x 0.5 in         => 0.5 in * 160 dpi = 80x80 px   

hdpi         -> 240 dpi          => 0.5 x 0.5 in         => 0.5 in * 240 dpi = 120x120 px  

xhdpi        -> 320 dpi          => 0.5 x 0.5 in         => 0.5 in * 320 dpi = 160x160 px  

xxhdpi       -> 480 dpi          => 0.5 x 0.5 in         => 0.5 in * 480 dpi = 240x240 px 

xxxhdpi      -> 640 dpi          => 0.5 x 0.5 in         => 0.5 in * 640 dpi = 320x320 px  



SDP - a scalable size unit - basically it is not a unit, but dimension resources for different screen size.

Try the sdp library from Intuit. It's very handy to solve unit problems, and you can quickly support multiple screens .

用法

android:paddingBottom="@dimen/_15sdp" for positive and android:layout_marginTop="@dimen/_minus10sdp" for negative sdp sdp

It has equivalent value in dp for each size in values-sw<N>dp folders (sw = smallestWidth).

Attention

Use it carefully! In most cases you still need to design a different layout for tablets.

<LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="@dimen/_minus10sdp"
          android:paddingBottom="@dimen/_15sdp"
          android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:includeFontPadding="false"
                    android:text="♡"
                    android:textColor="#ED6C27"
                    android:textSize="@dimen/_70sdp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:includeFontPadding="false"
                    android:text="U"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/_70sdp" />
            </LinearLayout>

You can use db for text size, but I prefer ssp for text size.

For more details, check the library GitHub page .




定義

px或dot是物理屏幕上的像素

dpi是物理屏幕上每英寸的像素數,代表顯示器的密度。

Android給出了幾種密度的別名

  • ldpi(低)〜120dpi
  • mdpi(中)〜160dpi
  • hdpi(高)〜240dpi
    • 2015年的大部分設備都在這裡
  • xhdpi(超高)〜320dpi
    • 蘋果iPhone 4/5/6,Nexus 4
  • xxhdpi(超高)〜480dpi
    • Nexus 5
  • xxxhdpi(額外超高)〜640dpi

dipdp密度不相關的像素 ,即它們對應於更多或更少的像素,具體取決於物理密度。

  • 1dp = 1px在mdpi上

spsip是一個與尺度無關的像素 。 在“設置” >“ 輔助功能”中打開“ 大文本”選項時,它們會被縮放

  • 1sp = 1dp
  • 1sp = 1.2dp,可訪問大文本

要使用什麼?

sp用於文本大小。

使用DP的一切。




以下是Android使用的公式:

px = dp *(dpi / 160)

其中dpi是以下屏幕密度之一。 有關所有可能的密度清單, 請點擊此處

它定義了“DENSITY_ *”常量。

  • ldpi(低)〜120dpi
  • mdpi(中)〜160dpi
  • hdpi(高)〜240dpi
  • xhdpi(超高)〜320dpi
  • xxhdpi(超高)〜480dpi
  • xxxhdpi(額外超高)〜640dpi

這裡採取。

如果你知道你的屏幕dpi,這將解決翻譯px和dp之間的許多困惑。

因此,假設您需要60dp的圖像作為hdpi屏幕,那麼60dp的物理像素大小為:

px = 60 * (240 / 160)



sp = scale independent pixel

dp = density independent pixels

dpi = density pixels

I have gone through the above answers...not finding them exactly correct. sp for text size, dp for layout bounds - standard. But sp for text size will break the layout if used carelessly in most of the devices.

sp take the textsize of the device, whereas dp take that of device density standard( never change in a device) Say 100sp text can occupies 80% of screen or 100% of screen depending on the font size set in device

You can use sp for layout bounds also, it will work :) No standard app use sp for whole text

Use sp and dp for text size considering UX.

  • Dont use sp for text in toolbar( can use android dimens available or dp)
  • Dont use sp for text in small bounded buttons, very smaller text, etc

Some people use huge FONT in their phone for more readability, giving them small hardcoded sized text will be an UX issue. Put sp for text where necessary, but make sure it won't break the layout.




dpdip 。 將它用於一切(邊距,填充等)。

僅為{text-size}使用sp

為了在不同的屏幕密度上獲得相同的尺寸,Android會在運行時將這些單元轉換為像素,因此您不需要做任何棘手的數學運算。

查看不同屏幕尺寸的pxdpsp之間的差異。

來源: Android編程:大書呆子牧場指南




任何與文字和外觀大小相關的東西都必須使用sppt 。 然而,與控件大小,佈局等相關的任何內容都必須與dp一起使用。

您可以在其位置使用dpdip




The screen of a mobile phone is made up of thousands of tiny dots known as pixels (px) . A pixel is the smallest element which goes to make the picture. The more the number of pixels to make a picture or wording, the sharper it becomes and makes the smartphone screen more easily readable.

Screen resolution is measured in terms of number of pixels on the screen. Screen resolution is a commonly-used specification when buying a device, but it's actually not that useful when designing for Android because thinking of screens in terms of pixels ignores the notion of physical size, which for a touch device is really really important.

Density independent pixel (dp or dip) allow the designer to create assets that appear in a expected way, no matter the resolution or density of target device.

A density independent pixel (dp or dip) is equal to one pixel at the baseline density or 160 dpi (dots per inch).

1 px/1dp = 160 dpi/160 dpi

2 px/1dp = 320 dpi(2x)/160 dpi

哪裡,

dpi is dots per inch

So, at 320 dpi, 1 dp is equal to 2 px.

px/dp = dpi/160dpi

Dots per inch (dpi) is a measure of the sharpness (that is, the density of illuminated points) on a display screen. The dots per inch for a given picture resolution will differ based on the overall screen size since the same number of pixels are being spread out over a different space.

Working with density independent pixels help us to deal with a situation like where you have two devices with same pixel resolution, but differing amount of space. Suppose in a case, a tablet and phone has the same pixel resolution 1280 by 800 pixels (160 dpi) and 800 by 1280 pixels (320 dpi) respectively.

Now because a tablet is at baseline density (160 dpi) its physical and density independent pixels sizes are the same, 1280 by 800. The phone on the other hand has a higher pixel density, so it has half as many density independent pixels as physical pixels. So a phone has 400 by 640 density independent pixels. So using a density-independent pixel makes it easier to mentally picture that tablet has much more space than the phone.

Similarly, if you have two devices with similar screen size, but different pixel density, say one is 800 by 1280 pixels (320 dpi), and the other is 400 by 640 pixels (160 dpi), we don't need to define totally different layouts for these two devices as we can measure assets in terms of density independent pixel which is same for both devices.

800 by 1280 pixels (320dpi)=400 by 640 density independent pixel (dp)

400 by 640 pixels (160 dpi)=400 by 640 density independent pixel (dp)

Scale independent pixels(sp) is the preferred unit for font size. For accessibility purposes, Android allows users to customize their device's font size. Users that have trouble reading text can increase their device's font size. You can normally find this option in the display setting on your phone or tablet under font size. It's often also available through the accessibility settings.

With scale independent pixels, 16 sp is exactly the same as 16 dp when the device's font size is normal or 100%. But when device's font size is large, for example 125%, 16 sp will translate to 20 dp or 1.25 times 16.

If you use dp as the unit for font size, then that piece of text has a specific physical size no matter if the user has customize device's font size. Using sp units will make a better experience for people with impaired eyesight.

Reference : Udacity , Google




Screen size in Android is grouped into categories ldpi , mdpi , hdpi , xhdpi , xxhdpi and xxxhdpi . Screen density is the amount of pixels within an area (like inch) of the screen. Generally it is measured in dots-per-inch ( dpi ).

PX(Pixels):

  • our usual standard pixel which maps to the screen pixel. px is meant for absolute pixels. This is used if you want to give in terms of absolute pixels for width or height. 不建議。

DP/DIP(Density pixels / Density independent pixels):

  • dip == dp . In earlier Android versions dip was used and later changed to dp . This is alternative of px .

  • Generally we never use px because it is absolute value. If you use px to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size.

  • dp is highly recommended to use in place of px . Use dp if you want to mention width and height to grow & shrink dynamically based on screen sizes.

  • if we give dp/dip , android will automatically calculate the pixel size on the basis of 160 pixel sized screen.

SP(Scale independent pixels):

  • scaled based on user's font size preference. Fonts should use sp .

  • when mentioning the font sizes to fit for various screen sizes, use sp . This is similar to dp .Use sp especially for font sizes to grow & shrink dynamically based on screen sizes

Android Documentation says:

when specifying dimensions, always use either dp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi . An sp is the same base unit, but is scaled by the user's preferred text size (it's a scale-independent pixel), so you should use this measurement unit when defining text size




通過更改Settings->Accessibility->Large Text選項,可以在運行時查看通過官方文檔複製的答案提及的“ 用戶字體大小首選項 ”中提到的dpsp單位之間的差異。

Large Text選項強製文字變大1.3倍。

private static final float LARGE_FONT_SCALE = 1.3f;

這可能當然是供應商的依賴,因為它位於packages/apps/Settings




Links