Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1543963
  • 博文数量: 237
  • 博客积分: 5139
  • 博客等级: 大校
  • 技术积分: 2751
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-18 14:48
文章分类

全部博文(237)

文章存档

2016年(1)

2012年(4)

2011年(120)

2010年(36)

2009年(64)

2008年(12)

分类: 嵌入式

2011-06-22 16:13:48

Differences of px, sp, pt, dip, dp, in, and mm?
  When we start be build an UI, or make definition of the dimensions,
  It's an issue we will meet - what are the difference of px, sp, pt, dip, dp, in, and mm?
  Here are the definitions:
  px - Pixel
  Independent from devices corresponding to actual pixels on the screen. (e.g. HVGA: 320x480)
  pt - Points
  1/72 of an inch
  dip - Density-independent Pixel
  It's an abstract unit that is based on the physical density relative to a 160 dpi screen. That is to say, one "dp" is one "px" on a 160 dpi screen. The ratio of dp-to-pixel changes with the screen density. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
  dp
  Compiler recognize it as "dip".
  sp - Scale-independent Pixel
   acts like an "dp" unit, but can be also scaled by the user's font size preference that will be adjusted for both the screen density and preference.
   is recommend when trying specifying font sizes,
  in -Inch
  The size that is based on the physical size of the screen.
  mm -Millimeter
  The size that is based on the physical size of the screen.

dip: device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA 推荐使用这个,不依赖像素。

px: pixels(像素). 不同设备显示效果相同,一般我们HVGA代表320x480像素,这个用的比较多。

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

sp: scaled pixels(放大像素). 主要用于字体显示best for textsize。

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

关于换算(以 sp 和 pt 为例)
查看 TextView 等类的源码,可知:

case COMPLEX_UNIT_PX:
return value;
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
case COMPLEX_UNIT_PT:
return value * metrics.xdpi * (1.0f/72);

--------------------------
scaledDensity = DENSITY_DEVICE / (float) DENSITY_DEFAULT;
xdpi = DENSITY_DEVICE;

--------------------------
DENSITY_DEFAULT = DENSITY_MEDIUM = 160;

============================================
所以: 假设 pt 和 sp 取相同的值 1,则可设 1pt 和 1sp 之间系数为 x,

1 * DENSITY_DEVICE / 72 = x * 1 * DENSITY_DEVICE / 160 =>
x = 160 / 72 = 2.2222

也就是说在 Android 中, 1pt 大概等于 2.22sp

===================================================================
什么是Dip和Sp

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

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

px(像素):屏幕上的点。

in(英寸):长度单位。

mm(毫米):长度单位。

pt(磅):1/72英寸。

dp(与密度无关的像素):一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px。

dip:与dp相同,多用于android/ophone示例中。

sp(与刻度无关的像素):与dp类似,但是可以根据用户的字体大小首选项进行缩放。

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



android 获取屏幕的高度和宽度

Android中的DisplayMetrics 定义了屏幕的一些属性,可以通过 getMetrics 方法得到当前屏幕的DisplayMetrics 属性,从而取得屏幕的宽和高。下面我们将获得的屏幕的宽和高显示在屏幕上。

/*------------获取屏幕的高度和宽度----------------*/    
//定义DisplayMetrics 对象  
setContentView(R.layout.main);  
 DisplayMetrics  dm = new DisplayMetrics();   //取得窗口属性  
getWindowManager().getDefaultDisplay().getMetrics(dm);     
//窗口的宽度  
int screenWidth = dm.widthPixels;    
 //窗口高度  
int screenHeight = dm.heightPixels;   

怎样把pix转化为dp
int pixel = 120; final float scale = getResources().getDisplayMetrics().density; int dip = (int) (pixel* scale + 0.5f);
阅读(2415) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~