免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1952 | 回复: 0
打印 上一主题 下一主题

android中pix, dp [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-21 08:41 |只看该作者 |倒序浏览
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
  It acts like an "dp" unit, but it can be also scaled by the user's font size preference that will be adjusted for both the screen density and preference.
  It 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);
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP