Example usage for android.content.res Resources getSystem

List of usage examples for android.content.res Resources getSystem

Introduction

In this page you can find the example usage for android.content.res Resources getSystem.

Prototype

public static Resources getSystem() 

Source Link

Document

Return a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).

Usage

From source file:Main.java

public static float getScreenLargestWidth() {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();

    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    float scaleFactor = metrics.density;

    float widthDp = widthPixels / scaleFactor;
    float heightDp = heightPixels / scaleFactor;

    return Math.max(widthDp, heightDp);
}

From source file:Main.java

/**
 * Convert DP to pixels using the device screen density
 *//*from   w w  w .  ja v a2  s. c  o  m*/
public static int dpToPx(int dp) {
    float density = Resources.getSystem().getDisplayMetrics().density;
    return Math.round(dp * density);
}

From source file:Main.java

public static float getScreenSmallestWidth() {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();

    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    float scaleFactor = metrics.density;

    float widthDp = widthPixels / scaleFactor;
    float heightDp = heightPixels / scaleFactor;

    return Math.min(widthDp, heightDp);
}

From source file:Main.java

public static Point getScreenSize() {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    return new Point(metrics.widthPixels, metrics.heightPixels);
}

From source file:Main.java

public static float getPxValue(int unit, float value) {
    return TypedValue.applyDimension(unit, value, Resources.getSystem().getDisplayMetrics());
}

From source file:Main.java

/**
 * Convert pixels to DP using the device screen density
 *//*from w w w .j a  va2  s  .c o m*/
public static float pxToDp(float px) {
    float densityDpi = Resources.getSystem().getDisplayMetrics().densityDpi;
    return pxToDp(px, densityDpi);
}

From source file:Main.java

public static int dpToPixels(int dp) {
    DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics();
    return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics));
    //return Math.round(dp * Resources.getSystem().getDisplayMetrics().density);
}

From source file:Main.java

public static Drawable getAndroidDrawable(final String drawableName) {
    final int resourceId = Resources.getSystem().getIdentifier(drawableName, "drawable", "android");
    if (resourceId == 0) {
        return null;
    } else {/*from ww  w  .  j  a  v a2 s.  c  om*/
        return Resources.getSystem().getDrawable(resourceId);
    }
}

From source file:Main.java

public static int sp2px(int sp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
            Resources.getSystem().getDisplayMetrics());
}

From source file:Main.java

/**
 * Converts dps to pixels/*from   w ww  . j  a v  a2 s  .c  o m*/
 * @param dps
 * @return pixels
 */

public static int toPx(Double dps) {
    final float scale = Resources.getSystem().getDisplayMetrics().density;
    return (int) (dps * scale + 0.5f);
}