`
ydbc
  • 浏览: 718437 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

我的Android进阶之旅------>Android常用计量单位(Dimension)的介绍(px,in,mm,pt,dp,dip,sp)

 
阅读更多

在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 thenameattribute (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'snamewill 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 atres/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="textview_height">25dp</dimen>
  <dimen name="textview_width">150dp</dimen>
  <dimen name="ball_radius">30dp</dimen>
  <dimen name="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"/>


当然,英文看的头疼的话,可以看下面的中文介绍


dipdeviceindependentpixels(设备独立像素).不同设备不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGAHVGAQVGA推荐使用这个,不依赖像素。

android上开发的程序将会在不同分辨率的手机上运行。为了让程序外观不至于相差太大,所以引入了dip的概念。比如定义一个矩形10x10dip.在分辨率为160dpi的屏上,比如G1,正好是10x10像素。而在240dpi的屏,则是15x15像素.换算公式为pixs=dips*(density/160).density就是屏的分辨率

这里要特别注意dip与屏幕密度有关,而屏幕密度又与具体的硬件有关,硬件设置不正确,有可能导致dip不能正常显示。在屏幕密度为160的显示屏上,1dip=1px,有时候可能你的屏幕分辨率很大如480*800,但是屏幕密度没有正确设置比如说还是160,那么这个时候凡是使用dip的都会显示异常,基本都是显示过小。

dip的换算:dipvalue=(int)(pxvalue/1.5+0.5)

dp与密度无关的像素):dip是一样的。一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp=1pxdipdp相同,多用于android/ophone示例中。

pxpixels(像素)屏幕上的点不同设备不同的显示屏显示效果相同这是绝对像素,是多少就永远是多少不会改变。一般我们HVGA代表320x480像素,这个用的比较多。

spscaledpixels—bestfortextsize(放大像素)与刻度无关的像素):主要处理字体的大小。dp类似,但是可以根据用户的字体大小首选项进行缩放。主要用于字体显示bestfortextsize。由此,根据google的建议,TextView的字号最好使用sp做单位,而且查看TextView的源码可知Android默认使用sp作为字号单位。

使用建议:

根据google的推荐,像素统一使用dip,字体统一使用sp

为了使用户界面能够在现在和将来的显示器类型上正常显示,建议大家始终使用sp作为文字大小的单位,将dip作为其他元素的单位。当然,也可以考虑使用矢量图形,而不是用位图

如果UI能够以sp为单位提供设计是最好的,如果设计中没有sp的概念,则开发人员也可以通过适当的换算取近似值。

过去,程序员通常以像素为单位设计计算机用户界面。例如,定义一个宽度为300像素的表单字段,列之间的间距为5个像素,图标大小为16×16像素等。这样处理的问题在于,如果在一个每英寸点数(dpi)更高的新显示器上运行该程序,则用户界面会显得很小。在有些情况下,用户界面可能会小到难以看清内容。

与分辨率无关的度量单位可以解决这一问题。Android支持下列所有单位。

其他单位:(不经常用)

pt(磅)point,是一个标准的长度单位,1pt1/72英寸,用于印刷业,非常简单易用;

Android中,1pt大概等于2.22sp

ininches(英寸):长度单位。

mmmillimeters(毫米):长度单位。


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:densityDip值表示每英寸有多少个显示点,与分辨率是两个概念

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转换的方法:

publicstaticintdip2px(Context context, float dipValue){
final float scale=context.getResources().getDisplayMetrics().density;
return (int)(dipValue*scale+0.5f);
}

publicstaticintpx2dip(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

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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics