V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
stdying
V2EX  ›  Android

px 和 dp 之间转换疑问

  •  
  •   stdying · 2015-11-02 14:32:03 +08:00 · 4359 次点击
    这是一个创建于 3107 天前的主题,其中的信息可能已经有所发展或是发生改变。
    /**
    * dip to px
    */
    public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
    }

    /**
    * px to dp
    */
    public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
    }
    }

    关于转换中 0.5f 起什么作用?
    2 条回复    2015-11-02 15:25:45 +08:00
    menc
        1
    menc  
       2015-11-02 14:44:18 +08:00 via iPhone
    四舍五入
    stdying
        2
    stdying  
    OP
       2015-11-02 15:25:45 +08:00
    @menc 对呀,要取整,谢谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3991 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:13 · PVG 13:13 · LAX 22:13 · JFK 01:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.