Example usage for android.content.res Resources getDisplayMetrics

List of usage examples for android.content.res Resources getDisplayMetrics

Introduction

In this page you can find the example usage for android.content.res Resources getDisplayMetrics.

Prototype

public DisplayMetrics getDisplayMetrics() 

Source Link

Document

Return the current display metrics that are in effect for this resource object.

Usage

From source file:Main.java

public static float getDipFromPixels(int pixel, Resources res) {
    return pixel / res.getDisplayMetrics().density;
}

From source file:Main.java

public static int pxToDp(int px, Resources resources) {
    float scale = resources.getDisplayMetrics().density;
    return (int) (px * scale + 0.5f);
}

From source file:Main.java

public static int scaleDpToPixels(double value, Resources res) {
    final float scale = res.getDisplayMetrics().density;
    return (int) Math.round(value * scale);
}

From source file:Main.java

public static int getPixelsFromDip(float dips, Resources res) {
    return Math.round(dips * res.getDisplayMetrics().density);
}

From source file:Main.java

public static int dp2px(int dp, Resources res) {
    final float scale = res.getDisplayMetrics().density;

    return (int) Math.ceil(dp * scale);
}

From source file:Main.java

static int convertToPx(int dp, Resources resources) {
    DisplayMetrics dm = resources.getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, dm);
}

From source file:Main.java

public static int dip2Px(Resources res, int dip) {

    DisplayMetrics dm = res.getDisplayMetrics();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, dm));
}

From source file:Main.java

public static int convertDpToPixel(float dp, Resources resources) {
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return (int) px;
}

From source file:Main.java

public static float convertPxToDp(Context context, float px) {
    Resources resources = context.getResources();
    return px / (resources.getDisplayMetrics().densityDpi / 160f);
}

From source file:Main.java

public static int spToPx(int textSizeSp, Resources resources) {
    final float density = resources.getDisplayMetrics().density;
    return (int) (0.5f + density * textSizeSp);
}