我的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: 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 screens 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. Scale-independent Pixels - This is like the dp unit, but it is also scaled by the users 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 users preference. Points - 1/72 of an inch based on the physical size of the screen. 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. Millimeters - Based on the physical size of the screen. 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.xmlThe filename is arbitrary. The dimen elements 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 /resourceselements:
dimen A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in), as described above.
attributes:
String. A name for the dimension. This will be used as the resource ID.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"/
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(毫米):长度单位。
状态栏和标题栏高个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
Android | 音视频方向进阶路线及资源合集 但是系统相机和系统控件VideoView的局限性都是可定制型太差,系统相机的图像分辨率,视频码率以及VideoView的进度条等.
字节卷动 You will never know how excellent you are unless you impel yourself once.
相关文章
- Android中UI线程与后台线程交互设计的5种方法
- 技术热点:Android hook技术浅析
- android AsyncTask介绍
- Android消息推送之GCM方式(一)
- android开发之微信支付功能的实现
- android MVC && MVP && MVVM分析和对照
- Android TextView支持的Html格式多元化
- 【Android 屏幕适配】屏幕适配通用解决方案 ② ( 自定义组件解决方案 | 需要解决的问题 : 设计稿坐标数据转为屏幕真实坐标数据 | 实现步骤 )
- 使用Visual Studio 2015开发Android 程序
- Android动态加载——加载未安装APK中的类&加载已安装APK中的类和资源
- android加载中对话框,循环,透明
- 一款简单易用的 Toast 组件,支持 Android&iOS
- 【Android 安装包优化】WebP 图片转换 ( 使用 iSparta 转换 WebP 图片格式 | Google 提供的 libwebp 库 )
- 我的Android进阶之旅------>Android中MediaRecorder.stop()报错 java.lang.RuntimeException: stop failed.
- 我的Android进阶之旅------>Android使用cmd窗口进行adb logcat时出现中文乱码问题的解决办法
- 我的Android进阶之旅------>Android嵌入图像InsetDrawable的用法
- 我的Android进阶之旅------>Android检测wifi连接状态
- 我的Android进阶之旅------>Android通过使用Matrix旋转图片来模拟碟片加载过程
- Android系统移植与调试之------->MTK 标准编译命令
- 我的Android进阶之旅------>RGB颜色查询对照表
- 我的Android进阶之旅------>Android Gusture 手势识别小案例
- 我的Android进阶之旅------>Android获取服务器上格式为JSON和XML两种格式的信息的小程序
- Android系统移植与调试之------->Android Sensor传感器系统架构初探