可能是重复问题:
在Android中将像素转换为dp
我想要将像素转换为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;
}