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 Resources createResources(Context context, AssetManager assetManager) {
    Resources superRes = context.getResources();
    return new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
}

From source file:Main.java

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

From source file:Main.java

/**
 * This method convets dp unit to equivalent device specific value in pixels. 
 * /*from   www. j  a v  a 2 s.c  om*/
 * @param dp A value in dp(Device 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 Pixels equivalent to dp according to device
 */
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 source file:Main.java

/**
 * Set default language/*from  ww w  . j  a v  a 2s.co  m*/
 * 
 * @param act
 * @param defLanguage
 */
public static void setDefaultLanguage(Activity act, String defLanguage) {
    if (defLanguage != null) {
        // Change locale settings in the app.
        Resources res = act.getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale(defLanguage);
        res.updateConfiguration(conf, dm);
    }
}

From source file:Main.java

public static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;//from w ww  .  jav a2s  .  co  m
}

From source file:Main.java

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

From source file:Main.java

/**
 * * This method converts device specific pixels to device 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 db
 * equivalent to px value//from w  w w .  ja v  a2s  .  c  o m
 */
public static float px2dp(Context context, float px) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;
}

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
 */// w w  w  . j  av  a 2 s.c  o  m
public static float convertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return dp;
}

From source file:Main.java

/**
 * * This method convets dp unit to equivalent device specific value in
 * pixels. * * @param dp A value in dp(Device 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 Pixels equivalent to dp according to device
 *//* w  w  w .  j  av a 2  s .  c  o  m*/
public static float dp2px(Context context, float dp) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;
}

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
 *//* w w  w.j a  v a 2s .  c  o m*/
public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    return px;
}