==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================

 

版权声明:本文为欧阳鹏原创文章,欢迎转载,转载请注明出处http://blog.csdn.net/ouyang_peng

 

在Android開發中常用到px,in,mm,pt,dp,dip,sp這幾個單位,你可不要混淆了
摘自:http://developer.android.com/guide/topics/resources/more-resources.html#Dimension的一段簡介

 

Dimension


 

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.

Note: A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one <resources> element.

file location:
res/values/filename.xml
The filename is arbitrary. The <dimen> element's name will be used as the resource ID.
resource reference:
In Java: R.dimen.dimension_name
In XML: @[package:]dimen/dimension_name
syntax:
<?xml version="1.0" encoding="utf-8"?><resources>
    <dimen
        name="dimension_name"
        >dimension</dimen></resources>
elements:
<resources>
Required. This must be the root node.

No attributes.

<dimen>
A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in), as described above.

attributes:

name
String. A name for the dimension. This will be used as the resource ID.
example:
XML file saved at res/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?><resources>
    <dimenname="textview_height">25dp</dimen>
    <dimenname="textview_width">150dp</dimen>
    <dimenname="ball_radius">30dp</dimen>
    <dimenname="font_size">16sp</dimen></resources>

This application code retrieves a dimension:

Resources res =getResources();float fontSize = res.getDimension(R.dimen.font_size);

This layout XML applies dimensions to attributes:

<TextView
    android:layout_height="@dimen/textview_height"
    android:layout_width="@dimen/textview_width"
    android:textSize="@dimen/font_size"/>

 

當然,英文看的頭疼的話,可以看下面的中文介紹

dip device independent pixels(設備獨立像素). 不同設備不同的顯示效果,這個和設備硬件有關,一般我們為了支持WVGA、HVGA和QVGA 推薦使用這個,不依賴像素。
在android上開發的程序將會在不同分辨率的手機上運行。為了讓程序外觀不至於相差太大,所以引入了dip的概念。比如定義一個矩形10 x 10dip.在分辨率為160dpi的屏上,比如G1,正好是10 x 10像素。而在240 dpi的屏,則是15 x 15像素.換算公式為pixs = dips * (density/160). density就是屏的分辨率
    這裡要特別注意dip與屏幕密度有關,而屏幕密度又與具體的硬件有關,硬件設置不正確,有可能導致dip不能正常顯示。在屏幕密度為160的顯示屏上,1dip=1px,有時候可能你的屏幕分辨率很大如480*800,但是屏幕密度沒有正確設置比如說還是160,那麼這個時候凡是使用dip的都會顯示異常,基本都是顯示過小。
     dip的換算: dip(value)=(int) (px(value)/1.5 + 0.5)
dp (與密度無關的像素):同dip是一樣的。一種基於屏幕密度的抽象單位。在每英寸160點的顯示器上,1dp = 1px。 dip 與dp相同,多用於android/ophone示例中。
px pixels(像素) 屏幕上的點,不同設備不同的顯示屏顯示效果相同,這是絕對像素,是多少就永遠是多少不會改變。一般我們HVGA代表320x480像素,這個用的比較多。
sp scaled pixels — best for text size (放大像素)(與刻度無關的像素):主要處理字體的大小。與dp類似,但是可以根據用戶的字體大小首選項進行縮放。主要用於字體顯示best for textsize。由此,根據google 的建議,TextView 的字號最好使用sp 做單位,而且查看TextView的源碼可知Android 默認使用sp 作為字號單位。
使用建議:
根據google的推薦,像素統一使用dip,字體統一使用sp
為了使用戶界面能夠在現在和將來的顯示器類型上正常顯示,建議大家始終使用sp作為文字大小的單位,將dip作為其他元素的單位。當然,也可以考慮使用矢量圖形,而不是用位圖
如果UI 能夠以sp 為單位提供設計是最好的,如果設計中沒有sp的概念,則開發人員也可以通過適當的換算取近似值。
過去,程序員通常以像素為單位設計計算機用戶界面。例如,定義一個寬度為300像素的表單字段,列之間的間距為5個像素,圖標大小為16×16像素等。這樣處理的問題在於,如果在一個每英寸點數(dpi)更高的新顯示器上運行該程序,則用戶界面會顯得很小。在有些情況下,用戶界面可能會小到難以看清內容。
與分辨率無關的度量單位可以解決這一問題。 Android支持下列所有單位。
其他單位:(不經常用)
pt(磅) point,是一個標準的長度單位,1pt=1/72英寸,用於印刷業,非常簡單易用;
在 Android 中,1pt 大概等於 2.22sp
in inches(英寸):長度單位。
mm millimeters(毫米):長度單位。

 
dip和px 的關係:

 

QVGA:    density=0.75;      densityDpi=120;     QVGA(240*320)

HVGA:    density=1.0;        densityDpi=160;     HVGA(320*480)

VGA:       density=1.0;        densityDpi=160;     VGA(480*640)

WVGA:   density=1.5;        densityDpi=240;     WVGA(480*800)

WQVGA:density=2.0;        densityDpi=120;     WQVGA(240*400)

 

PS:densityDpi值表示每英寸有多少個顯示點,與分辨率是兩個概念
densityDpi=120時
屏幕實際分辨率為240px*400px (兩個點對應一個分辨率)
狀態欄和標題欄高各19px或者25dip
橫屏是屏幕寬度400px 或者800dip,工作區域高度211px或者480dip
豎屏時屏幕寬度240px或者480dip,工作區域高度381px或者775dip
densityDpi=160時
屏幕實際分辨率為320px*533px (3個點對應兩個分辨率)
狀態欄和標題欄高個25px或者25dip
橫屏是屏幕寬度533px 或者800dip,工作區域高度295px或者480dip
豎屏時屏幕寬度320px或者480dip,工作區域高度508px或者775dip
densityDpi=240時
屏幕實際分辨率為480px*800px (一個點對於一個分辨率)
狀態欄和標題欄高個38px或者25dip
橫屏是屏幕寬度800px 或者800dip,工作區域高度442px或者480dip
豎屏時屏幕寬度480px或者480dip,工作區域高度762px或者775dip
apk的資源包中
當屏幕densityDpi=240時,使用hdpi 標籤的資源
當屏幕densityDpi=160時,使用mdpi標籤的資源
當屏幕densityDpi=120時,使用ldpi標籤的資源
不加任何標籤的資源是各種分辨率情況下共用的
佈局時盡量使用單位dip,少使用px
dp與px換算公式:
pixs =dips * (densityDpi/160).
dips=(pixs*160)/densityDpi
dp與px轉換的方法:
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale +0.5f);
}
public static int px2dip(Context context, float pxValue){
final float scale = context.getResource().getDisplayMetrics().density;
return (int)(pxValue / scale +0.5f);
}
VGA : 640*480
QVGA : 320*240
HVGA : 320*480
WVGA : 800*480
WQVGA : 480X272或400X240
分辨率(水平數×垂直數) 類型 比例
88×72 QQCIF 11:9
128×96 SUB-QCIF 4:3
128×128 知道的補上 1:1
160×120 QQVGA 4:3
176×144 QCIF 11:9
208×176 Sub-QVGA- 13:11
220×176 Sub-QVGA 5:4
240×176 Sub-QVGA+ 15:11
320×200 CGA 16:10
320×240 QVGA 4:3
352×288 CIF 11:9
640×360 nHD 4:3
400×240 WQVGA 5:3
400×320 WQVGA 5:4
480×240 WQVGA 2:1
480×272 WQVGA 16:9
480×320 HQVGA 3:2
640×480 VGA 4:3
640×350 EGA 64:35
720×480 VGA+ 3:2
768×576 PAL
800×480 WVGA 5:3
854×480 FWVGA 16:9
800×600 SVGA 4:3
960×540 QHD 16:9
960×640 DVGA 3:2
1024×600 WSVGA 128:75
1024×768 XGA 4:3
1280×768 WXGA 15:9
1280×800 WXGA 16:10
1280×960 UxGA/XVGA 4:3
1280×1024 SXGA 25:16
1400×1050 SXGA+ 4:3
1440×900 WXGA+ 16:10
1600×1024 WSXGA 25:16
1600×1050 WSXGA 32:21
1600×1200 USVGA/UXGA/UGA 4:3
1680×1050 WSXGA+ 16:10
1900×1200 UXGA 19:12
1920×1080 WSUVGA+(WSUGA/HDTV) 4:3
1920×1200 WUXGA 16:10
2048×1536 SUVGA(QXGA) 4:3
2560×1600 UWXGA 16:10
2560×2048 USXGA 5:4
3200×2400 QUXGA 4:3
3840×2400 WQUXGA 16:10

 

 

 


 

==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址http://blog.csdn.net/ouyang_peng

==================================================================================================

 

版权声明:本文为欧阳鹏原创文章,欢迎转载,转载请注明出处http://blog.csdn.net/ouyang_peng

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()