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 convertDIP2PX(Context context, int dip) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}

From source file:Main.java

public static int convertDpToPixel(Context context, int dp) {
    float density = context.getResources().getDisplayMetrics().density;
    return Math.round((float) dp * density);
}

From source file:Main.java

public static String getOrientation(Context context) {
    switch (context.getResources().getConfiguration().orientation) {
    case Configuration.ORIENTATION_LANDSCAPE:
        return "Landscape";
    case Configuration.ORIENTATION_PORTRAIT:
        return "Portrait";
    default://  w w  w . j  ava 2 s .c  o  m
        return "";
    }
}

From source file:Main.java

public static int px2sp(Context context, int px) {
    float density = context.getResources().getDisplayMetrics().scaledDensity;
    return (int) (px / density + 0.5f);
}

From source file:Main.java

public static int dp2px(Context context, int dp) {
    float density = context.getResources().getDisplayMetrics().density;
    return (int) (density * dp + 0.5f);
}

From source file:Main.java

public static int dp2px(float dpValue, Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    return (int) (dpValue * dm.density + 0.5f);
}

From source file:Main.java

public static int dpToPx(Context context, int dp) {
    float density = context.getResources().getDisplayMetrics().density;
    return Math.round((float) dp * density);
}

From source file:Main.java

public static int dp2Px(Context context, float dp) {
    float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dp * scale + 0.5f);
}

From source file:Main.java

public static int dip2px(Context contxt, int dip) {
    float density = contxt.getResources().getDisplayMetrics().density;
    return (int) (dip * density + 0.5);
}

From source file:Main.java

public static float px2dp(Context ctx, int px) {
    float density = ctx.getResources().getDisplayMetrics().density;
    float dp = px / density;

    return dp;/*w  ww . j  a va2s .c  om*/
}