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

Android将dp单位转换为系数为0.5f的像素单位,android,android-layout,Android,Android Layout,我读过关于将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; //

我读过关于将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...

这是为了使事情顺利进行。刻度可以是十进制(如1.5)。这意味着产品可能不是一个整数。将.5相加,然后转换为int,可以确保如果数字在两个整数之间超过一半,则该数字向上舍入;如果该数字小于一半,则向下舍入。

将浮点数转换为整数将使它们失效。 0.5f是数字的四舍五入:

x = (int) 3.9
print x // 3



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