可能重复:
在android中将像素转换为dp
我正在尝试将像素转换为 dp。公式是什么?
让我们将 640 和 480 转换为 dp。文档这样说
dp 单位到屏幕像素的转换很简单:px = dp * (dpi / 160)
但我认为这不是我需要的(而且我不知道如何使用它)。我想我只需要forumla。我已经准备好代码:
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;
}