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 int getPixelsHeight(Activity context) {
    DisplayMetrics dm = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels;
}

From source file:Main.java

public static DisplayMetrics getDisplayMetrics(View view) {
    DisplayMetrics metrics = new DisplayMetrics();
    Activity host = (Activity) view.getContext();
    host.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

From source file:Main.java

public static Bitmap downSampleBitmap(Uri uri, Activity act, Boolean needRotate) {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    act.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    Resources r = act.getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics()); // 50: magic num
    int targetWidth = displaymetrics.heightPixels;
    int targetHeight = displaymetrics.widthPixels - px;

    Bitmap resizedBitmap = decodeSampledBitmap(uri, targetWidth, targetHeight, act);
    Bitmap returnBitmap = null;//from  w  w w.j a  v a 2s.c  o m
    ExifInterface exif;
    try {
        float degree = 0;
        exif = new ExifInterface(uri.toString());
        int orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        if (resizedBitmap != null && needRotate) {
            degree = getDegree(orient);
            if (degree != 0) {
                returnBitmap = createRotatedBitmap(resizedBitmap, degree);
            }
            returnBitmap = returnBitmap == null ? resizedBitmap : returnBitmap;
        }
    } catch (IOException e) {
        Log.v(TAG, "not found file at downsample");
        e.printStackTrace();
    }
    return returnBitmap;
}

From source file:Main.java

/**
 * <p>//w w  w .ja va 2 s.co m
 * This method takes in the WindowManager and returns the Screen Dimensions in pixels.</br></br>
 * </p>
 * <p>To get width (getWindowDisplaySize(wm).x) or height (getWindowDisplaySize(wm).y)</p>
 *
 * @return WindowDisplaySize
 */
public static Point getWindowDisplaySize(WindowManager wm) {
    Display display;
    Point WindowDisplaySize = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        display = wm.getDefaultDisplay();
        display.getSize(WindowDisplaySize);
    } else {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;
        WindowDisplaySize.set(width, height);
    }
    return WindowDisplaySize;
}

From source file:Main.java

public static int getImageButtonSize(Activity activity) {
    if (mImageButtonSize == -1) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_LOW:
            mImageButtonSize = 16;/*ww w .  j av  a 2s.c  o m*/
            break;
        case DisplayMetrics.DENSITY_MEDIUM:
            mImageButtonSize = 32;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mImageButtonSize = 48;
            break;
        default:
            mImageButtonSize = 32;
        }
    }

    return mImageButtonSize;
}

From source file:Main.java

public static int[] getRealScreenSize(Context context) {

    int[] size = new int[2];

    WindowManager w = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display d = w.getDefaultDisplay();/*from  w ww.j a v a2s.  c om*/
    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) {
        }
    } else if (Build.VERSION.SDK_INT >= 17) {// includes window decorations (statusbar bar/menu bar)
        Point realSize = new Point();
        d.getRealSize(realSize);
        widthPixels = realSize.x;
        heightPixels = realSize.y;
    }

    size[0] = widthPixels;
    size[1] = heightPixels;
    return size;
}

From source file:Main.java

public static DisplayMetrics getScreenPix(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    return dm;// w w w . jav  a  2 s .c  o  m
}

From source file:Main.java

public static int[] getWidthAndHeight(Context context) {
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metrics = new DisplayMetrics();
    manager.getDefaultDisplay().getMetrics(metrics);
    return new int[] { metrics.widthPixels, metrics.heightPixels };
}

From source file:Main.java

/**
 * get screen width//w ww. jav a  2  s .co  m
 *
 * @param activity activity
 * @return screen width
 */
public static int getWidthPixels(Activity activity) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.widthPixels;
}

From source file:Main.java

/**
 * get screen height/* w w w .  ja va 2s .  co  m*/
 *
 * @param activity activity
 * @return screen height
 */
public static int getHeightPixels(Activity activity) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.heightPixels;
}