Example usage for android.content Context getResources

List of usage examples for android.content Context getResources

Introduction

In this page you can find the example usage for android.content Context getResources.

Prototype

public abstract Resources getResources();

Source Link

Document

Returns a Resources instance for the application's package.

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;/*from www. j  a  v a 2  s  .  c  o m*/
}

From source file:Main.java

public static int px2Dip(Context context, float pxValue) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
}

From source file:Main.java

public static int getScreenHeight(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    return dm.heightPixels;
}

From source file:Main.java

public static int px2dip(Context context, float pxValue) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5F);
}

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 int getIntOfRes(Context context, @IntegerRes int res) {
    return context.getResources().getInteger(res);
}

From source file:Main.java

public static int screenWidth(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    return dm.widthPixels;
}

From source file:Main.java

public static int screenHeight(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics dm = resources.getDisplayMetrics();
    return dm.heightPixels;
}

From source file:Main.java

/**
 * dp to px/*w  ww .  ja  va  2 s.com*/
 *
 * @param dp
 * @param context
 * @return
 */
public static int dpToPx(Context context, int dp) {
    return (int) (context.getResources().getDisplayMetrics().density * dp + 0.5f);
}

From source file:Main.java

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