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 getRawSize(int unit, float size, Context context) {
    Resources r;
    r = context.getResources();/*from  w  w  w.jav  a 2  s .  c o  m*/
    return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}

From source file:Main.java

/**
 * Convert Dp to Pixel/*from  w w w  .ja  v  a 2s.  c  o  m*/
 */
public static int dpToPx(float dp, Resources resources) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.getDisplayMetrics());
    return (int) px;
}

From source file:Main.java

public static float dpToPx(Resources res, float dp) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
    return px;/*  w  w  w.ja  v  a  2  s. c om*/
}

From source file:Main.java

/**
 * Converts dps to pixels nicely.//  w w  w . j  a  va 2 s  .c o  m
 * @param context the Context for getting the resources
 * @param dp dimension in dps
 * @return dimension in pixels
 */
protected static int dpToPixel(Context context, float dp) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return (int) (dp * (metrics.densityDpi / 160f));
}

From source file:Main.java

/**
 * This method converts device specific pixels to density independent pixels.
 *
 * @param px      A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
 *///  ww w  . ja va 2  s  .  com
public static float convertPixelToDp(Context context, float px) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return px / (metrics.densityDpi / 160f);
}

From source file:Main.java

public static int dpToPx(Resources res, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, res.getDisplayMetrics());
}

From source file:Main.java

public static float dipToPx(Resources res, float dip) {
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, res.getDisplayMetrics());
    return px;// w  w  w  . j a v  a  2 s.c o m
}

From source file:Main.java

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

From source file:Main.java

/**
 * This method converts dp unit to equivalent pixels, depending on device density.
 *
 * @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent px equivalent to dp depending on device density
 *//*from   ww  w. j a  v  a2  s . co m*/
public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return dp * (metrics.densityDpi / (float) DisplayMetrics.DENSITY_MEDIUM);
}

From source file:Main.java

/**
 * This method converts device specific pixels to density independent pixels.
 *
 * @param px A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent dp equivalent to px value
 *///from   w  ww. ja v a  2s .c  o  m
public static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return px / (metrics.densityDpi / (float) DisplayMetrics.DENSITY_MEDIUM);
}