Wednesday, 22 March 2017

Utilmethod to convert SP to PX

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

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...