代码如下:
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 }