[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



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. Not recommended.

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




在哪里可以使用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。




通过更改Settings->Accessibility->Large Text选项,可以在运行时查看通过正式文档复制的答案提及的“ 用户字体大小首选项 ”中提到的dpsp单位之间的差异。

Large Text选项强制文字变大1.3倍。

private static final float LARGE_FONT_SCALE = 1.3f;

这可能当然是供应商的依赖,因为它位于packages/apps/Settings




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




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  



以下是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)



基本上,px适用的唯一时间就是一个像素,这就是说,如果您希望屏幕上只有一个像素,就像分割器一样:

在> 160 dpi时,您可能会获得2-3像素,

在> 120dpi时,它舍入为0。




任何与文字和外观大小相关的东西都必须使用sppt 。 然而,与控件大小,布局等相关的任何内容都必须与dp一起使用。

您可以在其位置使用dpdip




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

注意

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 .




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编程:大书呆子牧场指南




px像素 - 每个比例点对应屏幕上的实际像素。

以英寸为单位 - 基于屏幕的物理尺寸。

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



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或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的一切。






Related