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 float sp2px(Context context, float size) {
    return size * context.getResources().getDisplayMetrics().scaledDensity;
}

From source file:Main.java

public static int dpTopx(int dp, Context ctx) {
    DisplayMetrics displayMetrics = ctx.getResources().getDisplayMetrics();
    return (int) ((dp * displayMetrics.density) + 0.5);
}

From source file:Main.java

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

From source file:Main.java

public static float px2sp(Context context, float size) {
    return size / context.getResources().getDisplayMetrics().scaledDensity;
}

From source file:Main.java

public static ColorStateList color(Context ctx, int color) {
    return (ColorStateList) ctx.getResources().getColorStateList(color);
}

From source file:Main.java

public static int convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return Math.round(px);
}

From source file:Main.java

public static int px2dp(Context context, float px) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (px / scale + 0.5);
}

From source file:Main.java

public static float px2dp(Context context, int pxVal) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (pxVal / scale);
}

From source file:Main.java

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

From source file:Main.java

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