可能的重复:
在android中将像素转换为dp https://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android
我正在尝试将像素转换为 dp。公式是什么?
让我们将 640 和 480 转换为 dp。文档是这样说的
dp单位到屏幕像素的转换很简单:px = dp * (dpi / 160)
但我认为这不是我需要的(而且我不知道如何使用它)。我想我只需要论坛。我已经准备好了代码:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi)
{
case DisplayMetrics.DENSITY_LOW:
int sixForty = ?
int fourEighty = ?
break;
case DisplayMetrics.DENSITY_MEDIUM:
int sixForty = ?
int fourEighty = ?
break;
case DisplayMetrics.DENSITY_HIGH:
int sixForty = ?
int fourEighty = ?
break;
}
您无需尝试从屏幕的密度分类推断 dp 转换系数,只需直接查询即可:
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float logicalDensity = metrics.density;
然后,逻辑密度将包含您需要乘以 dp 以获得设备屏幕的物理像素尺寸的因子。
int px = (int) Math.ceil(dp * logicalDensity);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)