dip2px 和 px2dip +0.5f是什么鬼?
来源:互联网 发布:c语言编译程序的软件 编辑:程序博客网 时间:2024/06/10 17:30
public static int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;return (int)(dipValue * scale + 0.5f); //+0.5是为了向上取整
}
public static int px2dip(Context context, float pxValue){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue / scale + 0.5f);//+0.5是为了向上取整
}
看官网是如何解释的:https://developer.android.com/guide/practices/screens_support.html
Converting dp units to pixel units
The DisplayMetrics.density field specifies the scale factor you must use to convert dp units to pixels, according to the current screen density. On a medium-density screen, DisplayMetrics.density equals 1.0; on a high-density screen it equals 1.5; on an extra-high-density screen, it equals 2.0; and on a low-density screen, it equals 0.75. This figure is the factor by which you should multiply the dp units on order to get the actual pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number, when converting to an integer.) For more information, refer to the DisplayMetrics class.
翻译过来就是:
displaymetrics.density字段指定比例因子必须使用转换DP单位像素,根据当前屏幕的密度。在中等密度屏幕,displaymetrics.density等于1;在高密度的屏幕,它等于1.5;在超高密度的屏幕,它等于2;在低密度的屏幕,它等于0.75。这个数字的因素,你应该乘DP单位为当前屏幕获得实际的像素数。(然后添加0.5f绕图到最接近的整数,当转换成一个整数。)的更多信息,请参阅displaymetrics类。
- dip2px 和 px2dip +0.5f是什么鬼?
- F# 2.0 & F#是什么
- F# 2.0 & F#是什么
- 如果你看到“f()();”第一反应是什么?
- CRF中的f(x,y)是什么
- SUID和SGID是什么鬼
- Android中dip(dp)与px之间单位转换 dip2px dp转px 无context算法(以及获取获取屏幕宽度和高度)
- %.2f 和 %1.2f的区别
- (转)tail -f 和 -F 的用法
- tail -f 和tail -F的区别
- tail -f 和 tail -F的区别
- F# 和Silverlight 4
- 问题 F: 因子和
- %f和%lf
- f:Form 和 SimpleForm
- Android drawable 的setBounds(0, 5, dip2px(20), dip2px(20)) 方法
- C# as和is是什么鬼
- 指针和引用究竟是什么鬼?
- springMVC 事物 不回滚 事物
- Objective-C 函数
- iOS byte byte数组的相关操作
- 不知道
- Windows中mysql数据库中文乱码永久解决方法
- dip2px 和 px2dip +0.5f是什么鬼?
- 基于InstallShield2013LimitedEdition的安装包制作
- 点击linearLayout,拨打手机系统电话
- springMVC-主题
- 驱动、固件
- adapter
- Android进阶图片处理内存优化系列之图片墙案例
- Java miscellany
- 正则表达式学习笔记