Example usage for android.view Display getMetrics

List of usage examples for android.view Display getMetrics

Introduction

In this page you can find the example usage for android.view Display getMetrics.

Prototype

public void getMetrics(DisplayMetrics outMetrics) 

Source Link

Document

Gets display metrics that describe the size and density of this display.

Usage

From source file:Main.java

public static int getDPI() {
    if (sContext != null) {
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager wm = ((Activity) sContext).getWindowManager();
        if (wm != null) {
            Display d = wm.getDefaultDisplay();
            if (d != null) {
                d.getMetrics(metrics);
                return (int) (metrics.density * 160.0f);
            }/*from w w w  . j av a  2s  .co  m*/
        }
    }
    return -1;
}

From source file:Main.java

public static boolean shouldScaleDownBitmap(Context context, Bitmap bitmap) {
    if (context != null && bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        return ((width != 0 && width / bitmap.getWidth() < 1)
                || (height != 0 && height / bitmap.getHeight() < 1));
    }//from   w w  w  .ja va  2s .  c  om
    return false;
}

From source file:Main.java

public static int getScreenWidth(Activity ctx) {
    Display display = ctx.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float density = ctx.getResources().getDisplayMetrics().density;
    return Math.round(outMetrics.widthPixels);

}

From source file:Main.java

public static DisplayMetrics getScreenMetrics(Activity activity) {

    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    return outMetrics;
}

From source file:Main.java

public static int getScreenHeight(Activity activity) {

    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    return outMetrics.heightPixels;
}

From source file:Main.java

public static DisplayMetrics getDeviceDisplayMetrics(Context context) {
    android.view.WindowManager windowsManager = (android.view.WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    android.view.Display display = windowsManager.getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    return outMetrics;
}

From source file:Main.java

public static float getScreenDensity(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.density;
}

From source file:Main.java

public static int[] getScreenSize(Context context) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
        try {//from   w  w w  .ja v  a2s  . c o  m
            widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
            widthPixels = realSize.x;
            heightPixels = realSize.y;
        } catch (Exception ignored) {
        }
    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

public static int[] getRealScreenSize(Activity activity) {
    int[] size = new int[2];
    int screenWidth = 0, screenHeight = 0;
    WindowManager w = activity.getWindowManager();
    Display d = w.getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    screenWidth = metrics.widthPixels;//w w  w  .j a  v a 2 s .c o m
    screenHeight = metrics.heightPixels;
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
        try {
            screenWidth = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
            screenHeight = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
        } catch (Exception ignored) {
        }
    // includes window decorations (statusbar bar/menu bar)
    if (Build.VERSION.SDK_INT >= 17)
        try {
            Point realSize = new Point();
            Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
            screenWidth = realSize.x;
            screenHeight = realSize.y;
        } catch (Exception ignored) {
        }
    size[0] = screenWidth;
    size[1] = screenHeight;
    return size;
}

From source file:Main.java

public static int getScreenWidth(@NonNull Context context) {
    Display display = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    return metrics.widthPixels;
}