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

public static float convertDpToPixel(Context context, float dp) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return px;// w ww  .  j  a  v a 2  s  . c om
}

From source file:Main.java

/**
 * This method converts device specific pixels to device independent pixels.
 * /*from w w  w.  j a va 2  s.  c  o m*/
 * @param px A value in px (pixels) unit. Which we need to convert into db
 * @param context Context to get resources and device specific display metrics
 * @return A float value to represent db equivalent to px value
 */
public static float ConvertPixelsToDp(float px, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;

}

From source file:Main.java

public static float getRawSize(Resources resources, int unit, float size) {
    return TypedValue.applyDimension(unit, size, resources.getDisplayMetrics());
}

From source file:Main.java

/**
 * get mobile screen resolution//  w  w w  .  j  a  v  a  2 s  . c o  m
 * 
 * @param context
 * @return
 */
public static String getScreenResolution(Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return metrics.heightPixels + "x" + metrics.widthPixels;
}

From source file:Main.java

public static float convertPixelsToDp(final float px, final Context context) {
    final Resources resources = context.getResources();
    final DisplayMetrics metrics = resources.getDisplayMetrics();
    final float dp = px / (metrics.densityDpi / 160f);
    return dp;//from   w  ww . j  a  v a 2 s . c om
}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

/**
 * initialize method, called inside the Chart.init() method. backwards
 * compatibility - to not break existing code
 *
 * @param res// w ww  . j  a  va 2  s .c  om
 */
@Deprecated
public static void init(Resources res) {

    mMetrics = res.getDisplayMetrics();

    // noinspection deprecation
    mMinimumFlingVelocity = ViewConfiguration.getMinimumFlingVelocity();
    // noinspection deprecation
    mMaximumFlingVelocity = ViewConfiguration.getMaximumFlingVelocity();
}

From source file:Main.java

public static int spToPx(Resources res, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, dp, res.getDisplayMetrics());
}

From source file:Main.java

public static float convertDpToPixel(float dp, Context context) {
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT);
    return px;//from w w  w. ja v  a  2s.  c  om
}