Android-将dp单位转换为像素单位,系数为0.5f

标签 android android-layout

我读了here关于将 dp 单位转换为像素单位。但我无法理解 0.5f。这个数字从哪里来,有什么用?

// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;

// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale

mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);

// Use mGestureThreshold as a distance in pixels...

最佳答案

将 float 转换为整数会使它们下降。 那个 0.5f 是四舍五入的数字:

x = (int) 3.9
print x // 3



x = (int) 3.9 + 0.5f
print x // 4

关于Android-将dp单位转换为像素单位,系数为0.5f,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52479567/

相关文章:

android - phonegap 应用程序的文档查看器

java - 如何从 Android 捕获静态 java.lang.UnsatisfiedLinkError 并向用户显示更好的错误消息?

android - android :entries in code? 的替代品是什么

android - 无法单击 DrawerLayout 后面的按钮

java - 是否可以设置操作栏主页按钮和应用程序 Logo 之间的填充?

Android 重复闹钟调用服务

android - view.getDrawingCache() 在 Android API 28 中被弃用

android - 在手机浏览器上单击图标时显示边框

Android:如何组织布局

android - 如何删除折叠工具栏的底部空白?