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 String getResolution(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    if (wm == null)
        return null;

    Display display = wm.getDefaultDisplay();
    if (display == null)
        return null;

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);/*w w w  .  ja v  a 2s  .c o  m*/
    return metrics.widthPixels + "x" + metrics.heightPixels;
}

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();/*from w w  w.j a  va 2  s. co m*/
    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 {
            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 float calculatorRatioScreen(Activity activity, boolean portrait) {
    DisplayMetrics disp = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(disp);
    int screenWidth, screenHeight;
    screenWidth = disp.widthPixels;/*from   w  w  w  .  ja v  a2s.c  o  m*/
    screenHeight = disp.heightPixels;
    Log.d("", "ratio screenWidth " + screenWidth + " screenHeight " + screenHeight);
    int defaultWidth = 480;
    int defaultHeight = 320;
    if (portrait) {
        defaultWidth = DEFAULT_SCREEN_WIDTH_PORTRAIT;
        defaultHeight = DEFAULT_SCREEN_HEIGHT_PORTRAIT;
    } else {
        defaultWidth = DEFAULT_SCREEN_WIDTH_LANSCAPE;
        defaultHeight = DEFAULT_SCREEN_HEIGHT_LANSCAPE;
    }
    float ratioByWidth = (float) screenWidth / defaultWidth;
    float ratioByHeight = (float) screenHeight / defaultHeight;
    float ratioReturn = Math.min(ratioByWidth, ratioByHeight);
    // isAlReady = true;
    return ratioReturn;
}

From source file:Main.java

@SuppressLint("NewApi")
public static Point getScreenRawSize(Display display) {
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
        Point outPoint = new Point();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        outPoint.x = metrics.widthPixels;
        outPoint.y = metrics.heightPixels;
        return outPoint;
    } else {//from ww  w.ja  v a 2  s  . c  om
        Point outPoint = new Point();
        Method mGetRawH;
        try {
            mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            outPoint.x = (Integer) mGetRawW.invoke(display);
            outPoint.y = (Integer) mGetRawH.invoke(display);
            return outPoint;
        } catch (Throwable e) {
            return new Point(0, 0);
        }
    }
}

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();/*from   w w  w  .j  av  a  2 s.  c om*/
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    screenWidth = metrics.widthPixels;
    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

private static void getDisplay(Context context) {
    if (width <= 0 || height <= 0) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;//w w  w.  jav a  2 s.c  o m
        height = dm.heightPixels;
    }
}

From source file:Main.java

public static int GetScreenWidthPx(final Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.widthPixels;
}

From source file:Main.java

public static int GetScreenHeightPx(final Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels;
}

From source file:Main.java

private static int getScreenHeight(Activity actvity) {
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    actvity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.heightPixels;
}

From source file:Main.java

static int calculateOrientation(Activity activity, int cameraId) {
    if (cameraId == NO_CAMERA)
        return 0;

    DisplayMetrics dm = new DisplayMetrics();
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int cameraRotationOffset = info.orientation;
    Log.w(TAG, "cameraRotationOffset = " + cameraRotationOffset);

    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int currentScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Log.w(TAG, "currentScreenRotation = " + currentScreenRotation);

    int degrees = 0;
    switch (currentScreenRotation) {
    case Surface.ROTATION_0:
        degrees = 0;/*w w w  . j  a  v a2  s.  c o  m*/
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }
    Log.w(TAG, "degrees = " + degrees);

    int orientation;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        orientation = (cameraRotationOffset + degrees) % 360;
        orientation = (360 - orientation) % 360;
    } else {
        orientation = (cameraRotationOffset - degrees + 360) % 360;
    }
    Log.w(TAG, "orientation = " + orientation);

    return orientation;
}