dp转px sp转px

dp转px

public static int dp2px(Context context, final float dpValue) {
    final float scale = context.getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

px转dp

public static int px2dp(Context context,final float pxValue) {
        final float scale = context.getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

sp转px

public static int sp2px(Context context,final float spValue) {
        final float fontScale=context.getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

px转sp

public static int px2sp(Context context,final float pxValue) {
        final float fontScale =context.getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
声明:原创文章,版权所有,转载请注明出处,https://litets.com。