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 Bitmap createDrawableFromView(Context context, View view) {

    Bitmap bit = null;//from www.ja v  a 2s  . c  o  m
    try {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        bit = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bit);
        view.draw(canvas);
    } catch (NullPointerException e) {
        Log.e("NullPointerException", e.toString());
        e.printStackTrace();
    }
    return bit;
}

From source file:Main.java

private static DisplayMetrics getDisplayMetrics(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    Display display = activity.getWindowManager().getDefaultDisplay();
    display.getMetrics(metrics);//from   w w w.  j a  va2s  .c  o  m
    return metrics;
}

From source file:Main.java

static int calculateOrientationHint(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, "OrientationHint cameraRotationOffset = " + cameraRotationOffset);

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

    int degrees = 0;
    switch (currentScreenRotation) {
    case Surface.ROTATION_0:
        degrees = 0;/*from   ww w.j ava  2 s.c om*/
        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, "OrientationHint degrees = " + degrees);

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

    return orientation;
}

From source file:Main.java

public static int getCameraDisplayOrientation(@NonNull Activity activity, @NonNull CameraInfo info) {
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    DisplayMetrics dm = new DisplayMetrics();

    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);

    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;/* w  w w  .  j  a  v  a  2 s .c om*/
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        return (360 - ((info.orientation + degrees) % 360)) % 360;
    } else {
        return (info.orientation - degrees + 360) % 360;
    }
}

From source file:Main.java

public static DisplayMetrics getDisplayMetrics(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics;
}

From source file:Main.java

public static Pair<Integer, Integer> getWindowDimensionsWithoutMargin(Context context,
        WindowManager windowManager, int dpMarginX, int dpMarginY) {

    dpMarginX *= 2;/*w  w w.j a va2  s .  c  om*/
    dpMarginY *= 2;

    DisplayMetrics displayMetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
    int screenHeight = displayMetrics.heightPixels;

    int marginX = dpToPx(context, dpMarginX);
    if (hasSoftNavigation(context)) {
        dpMarginY += 30;
    }
    int marginY = dpToPx(context, dpMarginY);

    Pair<Integer, Integer> dimensions = new Pair<Integer, Integer>(screenWidth - marginX,
            screenHeight - marginY);

    return dimensions;
}

From source file:Main.java

public static int[] getScreenSize(Context context, boolean useDeviceSize) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();//from   w w w  . j a v a2 s  . c o  m
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    if (!useDeviceSize) {
        size[0] = widthPixels;
        size[1] = heightPixels;
        return size;
    }

    // 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

private static DisplayMetrics getDisplayMetrics(Context context) {
    if (metrics != null) {
        return metrics;
    }//from   w ww . j  a  v  a2  s .  c om
    metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

From source file:Main.java

public static int[] getScreenSize(Context context, boolean useDeviceSize) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();//from   w  ww . j a  v  a2  s  . c  o m
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);
    // since SDK_INT = 1;
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    if (!useDeviceSize) {
        size[0] = widthPixels;
        size[1] = heightPixels - getStatusBarHeight(context);

        return size;
    }

    // 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

private static DisplayMetrics getDisplayMetrics(final Activity activity) {
    final DisplayMetrics metrics = new DisplayMetrics();
    final Display display = activity.getWindowManager().getDefaultDisplay();
    display.getMetrics(metrics);/*from  ww w . j a  va 2 s.c  o m*/
    return metrics;
}