Example usage for android.util DisplayMetrics DisplayMetrics

List of usage examples for android.util DisplayMetrics DisplayMetrics

Introduction

In this page you can find the example usage for android.util DisplayMetrics DisplayMetrics.

Prototype

public DisplayMetrics() 

Source Link

Usage

From source file:Main.java

/**
 * Pixels to dp./*  w  ww  .  j  a v  a 2  s . co m*/
 *
 * @param context the context
 * @param px the px
 * @return the int
 */
public static int PixelsToDp(Activity context, int px) {
    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) ((px / metrics.density) + 0.5);
}

From source file:Main.java

/**
 * Dp to pixels./*  w  w w.j  a  va  2  s  .co  m*/
 *
 * @param context the context
 * @param dp the dp
 * @return the int
 */
public static int dpToPixels(Activity context, int dp) {
    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) ((dp * metrics.density) + 0.5);
}

From source file:Main.java

public static float pixelsToDp(Context ctx, float px) {
    WindowManager wm = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE));
    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);
    float logicalDensity = metrics.density;
    return (float) Math.ceil(px * logicalDensity);
}

From source file:Main.java

public static DisplayMetrics getScreenSize(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    return dm;/*w  ww .  ja va  2s  . co m*/
}

From source file:Main.java

public static DisplayMetrics getScreenInfo(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics dm = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(dm);
    return dm;/*  ww w  . j  a  v a  2s .  com*/
}

From source file:Main.java

public static int getScreenWidth(Context context) {
    WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    wManager.getDefaultDisplay().getMetrics(metrics);
    return metrics.widthPixels;
}

From source file:Main.java

public static Integer getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);

    return metrics.widthPixels;
}

From source file:Main.java

public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(outMetrics);
    int width = outMetrics.widthPixels;
    return width;
}

From source file:Main.java

public static int getScreenHeight(Context context) {
    WindowManager wManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    wManager.getDefaultDisplay().getMetrics(metrics);
    return metrics.heightPixels;
}

From source file:Main.java

public static Integer getScreenHeight(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

    DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);

    return metrics.heightPixels;
}