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 int convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int px = Math.round(dp * (metrics.densityDpi / 160f)); // bit fuzzy, but probably won't be big issues.
    return px;//ww w.jav a 2 s  .c  o  m
}

From source file:Main.java

public static float dpToPx(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return dp * (metrics.densityDpi / 160f);
}

From source file:Main.java

public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;//from ww  w.j  av a 2s .c  o m
}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

public static float convertPixelsToDp(Context context, float pxValue) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return pxValue / (metrics.densityDpi / 160f);

}

From source file:Main.java

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

From source file:Main.java

public static float getDpi(Context context) {
    // will either be DENSITY_LOW, DENSITY_MEDIUM or DENSITY_HIGH
    //For Samsung Galaxy sIII : 320 dpi
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    int dpiClassification = metrics.densityDpi;
    return dpiClassification;
}

From source file:Main.java

public static int pixelsToDpi(Resources resources, int i) {
    return (int) TypedValue.applyDimension(1, (float) i, resources.getDisplayMetrics());
}

From source file:Main.java

public static float dpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return dp * (metrics.densityDpi / 160f);
}