【问题标题】:how can I set ImageView position in dpi fro android api 8?如何在 dpi 中为 android api 8 设置 ImageView 位置?
【发布时间】:2013-11-14 10:01:09
【问题描述】:

我的安卓应用支持 Android api 8

我想将一个对象x dpi 向左移动。

我该怎么做?

【问题讨论】:

    标签: android android-layout layout dpi


    【解决方案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-02-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 2013-10-05
      • 2015-02-23
      • 1970-01-01
      相关资源
      最近更新 更多