160*240 -------- 320*480 (手机屏幕尺寸不变为前提,密度由160变化为320)
px:实际像素 (160不变,原view的显示会减少一截)
dp:建议使用在view的width和height,会根据屏幕密度的变化自动进行转换(160*(320/160) 密度因子),dp与密度无关
sp:建议使用在view的字体大小,会根据屏幕密度的变化自动进行转换(160*(320/160)),sp除了与密度无关外,还与scale无关
in:英寸,屏幕的物理尺寸,即对角线的长度。 一英寸=2.54厘米
eg:三星note3 N9008 尺寸为5.7
dpi:Dots Per Inch (每英寸所打印的点数)的缩写
pt:表示一个点,是屏幕的一个物理长度,大小为1英寸的1/72
scaledDensity (字体缩放比例)。即 单位 sp 的 换算值。 一般用在设定字体大小中
如下内容转自:http://my.oschina.net/idip/blog/522965
当我们在资源文件里设置尺寸的时候多是用dp,那么Android的开发并不局限于我们所用的资源文件,有时候我们需要动态的管理视图的尺寸,view给我们提供了setPadding(left, top, right, bottom);但是此时的 int并不是dp,而是px,由自己来转换;下面给出转换的函数。
/**
* dp2px
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;//手机中展示density为2.0
return (int) (dpValue * scale + 0.5f);
}
/**
* px2dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
*根据设备信息获取当前分辨率下指定单位对应的像素大小;
* px,dip,sp -> px
*/
public float getRawSize(Context c, int unit, float size) {
Resources r;
if (c == null){
r = Resources.getSystem();
}else{
r = c.getResources();
}
return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}