dp与px之间的转换

代码如下:

 1 package com.example.fxvideo.utils;
 2 
 3 import android.content.Context;
 4 
 5 public class DensityUtils {
 6 
 7     /**
 8      * dp杞琾x
 9      */
10     public static int dp2px(Context ctx, float dp) {
11         float density = ctx.getResources().getDisplayMetrics().density;
12         int px = (int) (dp * density + 0.5f);// 4.9->5 4.4->4
13 
14         return px;
15     }
16 
17     public static float px2dp(Context ctx, int px) {
18         float density = ctx.getResources().getDisplayMetrics().density;
19         float dp = px / density;
20 
21         return dp;
22     }
23 }

 

posted on 2016-06-14 19:38  oooo呼呼  阅读(275)  评论(0编辑  收藏  举报