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 getScreenSize(Activity context) {
    DisplayMetrics dm = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm;/*from   ww  w  .  j  a va2 s. co m*/
}

From source file:Main.java

public static DisplayMetrics getDisplayMetric(Context context) {
    if (context != null) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = windowManager.getDefaultDisplay();
        display.getMetrics(displayMetrics);
        return displayMetrics;
    }//from   w w w. ja va2s . c  o  m
    return null;
}

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;

    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int currentScreenRotation = activity.getWindowManager().getDefaultDisplay().getRotation();

    int degrees = 0;
    switch (currentScreenRotation) {
    case Surface.ROTATION_0:
        degrees = 0;/*from 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;
    }

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

    return orientation;
}

From source file:Main.java

public static int getScreenWidthDP(Activity activity) {

    WindowManager wm = activity.getWindowManager();
    //wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display dp = wm.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    dp.getMetrics(dm);//from   w ww.  j a  va2 s .c o m

    float res = dm.widthPixels / dm.density;
    return (int) res;
}

From source file:Main.java

public static int getScreenHeightDP(Activity activity) {

    WindowManager wm = activity.getWindowManager();
    //wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    Display dp = wm.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    dp.getMetrics(dm);/*from   w w  w .j a  v  a2 s.c  om*/

    float res = dm.heightPixels / dm.density;
    return (int) res;
}

From source file:Main.java

public static int getWindowWidth(Activity activity) {
    if (mWidthPixels < 0) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        mWidthPixels = dm.widthPixels;// w  ww  . j a v a 2 s. c o  m
        mHeightPixels = dm.heightPixels;
    }
    return mWidthPixels;
}

From source file:Main.java

public static float getDimensionPixelSize(int unit, float value, Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    switch (unit) {
    case PX:/*from ww  w  .  j a  v a  2 s  . c  o  m*/
        return value;
    case DIP:
    case SP:
        return TypedValue.applyDimension(unit, value, metrics);
    default:
        throw new IllegalArgumentException("unknow unix");
    }
}

From source file:Main.java

public static void init(Context context) {
    if (context == null) {
        return;// w  ww  .  ja  va 2  s  . co  m
    }
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(dm);
    SCREEN_WIDTH_PIXELS = dm.widthPixels;
    SCREEN_HEIGHT_PIXELS = dm.heightPixels;
    SCREEN_DENSITY = dm.density;
    SCREEN_WIDTH_DP = (int) (SCREEN_WIDTH_PIXELS / dm.density);
    SCREEN_HEIGHT_DP = (int) (SCREEN_HEIGHT_PIXELS / dm.density);
}

From source file:Main.java

public static int getDisplayWidth(Context context) {
    if (displayWidth <= 0) {
        WindowManager wm = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        displayWidth = dm.widthPixels;/*from  w w w  .j a v  a  2  s.co m*/
    }
    return displayWidth;
}

From source file:Main.java

public static int getDisplayHeight(Context context) {
    if (displayHeight <= 0) {
        WindowManager wm = (WindowManager) context.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        displayHeight = dm.heightPixels;
    }// w  w w. j  av  a2  s .co m
    return displayHeight;
}