我读到关于将dp单位转换为像素单位的here。但是我不能理解0.5华氏度。这个数字是从哪里来的?它有什么用?

// 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,可以确保如果数字在两个整数之间超过一半,则该数字向上取整;如果小于一半,则该数字向下取整。

10-07 19:27
查看更多