private static float sDensity = Float.MAX_VALUE;
private static float sDensity4Fonts = Float.MAX_VALUE;
public static int convertSpToPx(int spSize) {
return Math.round((float) spSize / getDisplayDensity4Fonts());
}
private static DisplayMetrics getDisplayMetrics() {
Display display = ((WindowManager) API.getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
return metrics;
}
private static float getDisplayDensity4Fonts() {
synchronized (Helper.class) {
if (sDensity4Fonts == Float.MAX_VALUE) {
sDensity4Fonts = getDisplayMetrics().density * getFontScale();
}
return sDensity4Fonts;
}
}
No comments:
Post a Comment