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

public static DisplayMetrics getWindowMetrics(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

From source file:Main.java

public static DisplayMetrics getScreenSize(Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    display.getMetrics(metrics);/*from  w w w  .  j av a 2 s  .c om*/
    return metrics;
}

From source file:Main.java

public static int getHeigth(Activity activity) {
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels;
}

From source file:Main.java

/**
 * This Function convert the DP to Pixel.
 * //from w  ww  . j  av  a2 s  .c o m
 * See below for @params.
 */

public static int convertDPtoPX(Context ctx, int dp) {

    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float logicalDensity = metrics.density;
    int px = (int) (dp * logicalDensity + 0.5);
    System.out.println("DP to Pixel-->" + dp + " = " + px);
    return px;
}

From source file:Main.java

/**
 * Get the screen width./*from w  ww  .j av a  2  s. c o m*/
 *
 * @param context
 * @return the screen width
 */
public static int getScreenWidth(Context context) {
    if (context != null) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        return displayMetrics.widthPixels;
    } else {
        return 1080;
    }
}

From source file:Main.java

public static int getOrientation(Context context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    displayMetrics.widthPixels = 1280;//from  w  w  w .ja  v  a2s .c  o m
    displayMetrics.heightPixels = 720;
    try {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
    } catch (Exception e) {
        //do nothing
    }

    if (displayMetrics.heightPixels > displayMetrics.widthPixels) {
        return PORTRAIT;
    } else {
        return LANDSCAPE;
    }
}

From source file:Main.java

/**
 * Get the screen height./* www. j a v a 2 s .  c  o m*/
 *
 * @param context
 * @return the screen height
 */
public static int getScreenHeight(Context context) {
    if (context != null) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        return displayMetrics.heightPixels;
    } else {
        return 1920;
    }
}

From source file:Main.java

public static float getScreenDensity(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics.density;
}

From source file:Main.java

public static Pair<Integer, Integer> getScreenDimensions(Context paramContext) {
    WindowManager localWindowManager = (WindowManager) paramContext.getSystemService("window");
    DisplayMetrics localDisplayMetrics = new DisplayMetrics();
    localWindowManager.getDefaultDisplay().getMetrics(localDisplayMetrics);
    int i = Math.min(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
    int j = Math.max(localDisplayMetrics.widthPixels, localDisplayMetrics.heightPixels);
    return new Pair(Integer.valueOf(i), Integer.valueOf(j));
}

From source file:Main.java

public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);/*from   w w  w. j  a va 2 s  .  c o  m*/

    return metrics.widthPixels;
}