dp和px的转换

时间:2024-01-12 19:00:26
    /**
* dp转px
* @param context
* @param dp
* @return
*/
public static int dp2px(Context context, float dp){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(dp * scale + 0.5f);
}
/**
* px转dp
* @param context
* @param px
* @return
*/
public static int px2dp(Context context, float px){
final float scale = context.getResources().getDisplayMetrics().density;
return (int)(px / scale + 0.5f);
}