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

/**
 * This method converts dp unit to equivalent device specific value in pixels.
 *
 * @param ctx The current context//from ww  w . j  a va 2 s  .  co m
 * @param dp A value in dp (Device independent pixels) unit
 * @return float A float value to represent Pixels equivalent to dp according to device
 */
public static float convertDpToPixel(Context ctx, float dp) {
    Resources resources = ctx.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return dp * (metrics.densityDpi / 160f);
}

From source file:Main.java

private static int getResourceDisplayDensityDPI(Context context) {
    try {/*from   ww w.j av a 2s .c o m*/
        Resources resources = context.getResources();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        return displayMetrics.densityDpi;
    } catch (NullPointerException expected) {
        return DisplayMetrics.DENSITY_DEFAULT;
    }
}

From source file:Main.java

public static float pixel2dip(Context context, float n) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = n / (metrics.densityDpi / 160f);
    return dp;/* w ww  . ja va 2s.  co m*/

}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

/**
 * This method converts device specific pixels to device independent pixels.
 *
 * @param ctx The current context/*from   w ww  . j a v  a  2  s  .  c o m*/
 * @param px A value in px (pixels) unit
 * @return A float value to represent dp equivalent to px value
 */
public static float convertPixelsToDp(Context ctx, float px) {
    Resources resources = ctx.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return px / (metrics.densityDpi / 160f);

}

From source file:Main.java

public static int convertDipToPx(int dip, Resources resources) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, resources.getDisplayMetrics());
}

From source file:Main.java

public static void setLocale(Context ctx, String lang) {
    Locale locale;/*www.  j ava2  s . c  om*/

    /* if(lang.equals("zh") || lang.equals("zh-HK") || lang.equals("zh-")) locale = Locale.TRADITIONAL_CHINESE;
     else if(lang.equals("zh-CN")) locale = Locale.SIMPLIFIED_CHINESE;
     else    locale = new Locale(lang);*/

    locale = new Locale(lang);
    Resources res = ctx.getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
}

From source file:Main.java

public static boolean isOver600dp(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics displayMetrics = resources.getDisplayMetrics();
    return displayMetrics.widthPixels / displayMetrics.density >= 600;
}

From source file:Main.java

public static float dipToPixels(float dip, Resources resource) {

    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, resource.getDisplayMetrics());
    return px;/*from   w  ww .  j a v a  2 s . com*/

}