自由任我行

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

有的时候要用到像素,就需要将像素和dp长度进行转换,方法如下

 public static int pixelToDp(Context context, int pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
    
    public static int dip2px(Context context, float dipValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
        }

 

posted on 2015-10-14 10:11  自由任我行  阅读(156)  评论(0)    收藏  举报