0

我的 android 应用程序支持 Android api 8

我想将一个对象x dpi移到左边。

我该怎么做?

4

1 回答 1

1

您应该将给定屏幕的数量转换dip为正确的数量。pixels

这是您应该使用的公式:

pixels = dip * (density / 160)

(density / 160)部分被称为density scale factor. 您可以使用以下代码获取此比例因子。

float scale = getContext().getResources().getDisplayMetrics().density;

然后根据给定的倾角计算正确数量的像素并将其四舍五入:

int pixels (int) (dip * scale + 0.5f);

在函数形式中,它看起来像这样。

public int getPixelFromDip(float dip){
    final float scale = getContext().getResources().getDisplayMetrics().density;
    return (int) (dip * scale + 0.5f);
}

罗尔夫

于 2013-11-14T11:35:20.697 回答