Example usage for android.app Activity getWindowManager

List of usage examples for android.app Activity getWindowManager

Introduction

In this page you can find the example usage for android.app Activity getWindowManager.

Prototype

public WindowManager getWindowManager() 

Source Link

Document

Retrieve the window manager for showing custom windows.

Usage

From source file:Main.java

/**
 * Pixels to dp./*from w w w . j  a v  a2s.  c om*/
 *
 * @param context the context
 * @param px the px
 * @return the int
 */
public static int PixelsToDp(Activity context, int px) {
    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) ((px / metrics.density) + 0.5);
}

From source file:Main.java

/**
 * Dp to pixels./* www . j  ava2 s. co  m*/
 *
 * @param context the context
 * @param dp the dp
 * @return the int
 */
public static int dpToPixels(Activity context, int dp) {
    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return (int) ((dp * metrics.density) + 0.5);
}

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

public static int getDeviceHeigh(Activity activity) {
    if (dm == null) {
        dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    }/*from  ww  w .  ja  v a  2 s  . com*/
    return dm.heightPixels;
}

From source file:Main.java

public static int getDeviceWidth(Activity activity) {
    if (dm == null) {
        dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    }//w  w  w  .j  a v  a2 s.co  m
    return dm.widthPixels;
}

From source file:Main.java

public static float computeY(Activity activity, float val) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int h = displayMetrics.heightPixels;

    return val * ((float) h / 1200.0f);
}

From source file:Main.java

public static float getDensity(Activity context) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    return displayMetrics.density;
}

From source file:Main.java

/**
 * Method that get the Screen Size/*  w ww. j a v a 2 s  .  c om*/
 * @param activity Actual Activity
 * @return array type int 
 */
public static int[] getScreenSize(Activity activity) {

    //Obtain default display
    Display display = activity.getWindowManager().getDefaultDisplay();
    Point size = new Point();

    //Set Size in case of ICS
    if (android.os.Build.VERSION.SDK_INT >= 13) {
        display.getSize(size);
        return new int[] { size.x, size.y };
    } else {
        return new int[] { display.getWidth(), display.getHeight() };
    }
}

From source file:Main.java

/**
 * Get a BitmapDrawable from a local file that is scaled down
 * to fit the current Window size.// w w w. jav  a  2s. com
 */
@SuppressWarnings("deprecation")
public static BitmapDrawable getScaledDrawable(Activity a, String path) {
    Display display = a.getWindowManager().getDefaultDisplay();
    float destWidth = display.getWidth();
    float destHeight = display.getHeight();

    // read in the dimensions of the image on disk
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    float srcWidth = options.outWidth;
    float srcHeight = options.outHeight;

    int inSampleSize = 1;
    if (srcHeight > destHeight || srcWidth > destWidth) {
        if (srcWidth > srcHeight) {
            inSampleSize = Math.round((float) srcHeight / ((float) destHeight) * 3);
        } else {
            inSampleSize = Math.round((float) srcWidth / ((float) destWidth) * 3);
        }
    }

    options = new BitmapFactory.Options();
    options.inSampleSize = inSampleSize;

    Bitmap bitmap = BitmapFactory.decodeFile(path, options);
    return new BitmapDrawable(a.getResources(), bitmap);
}

From source file:Main.java

private static double getDisplayInches(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    float widthDpi = metrics.xdpi;
    float heightDpi = metrics.ydpi;

    float widthInches = widthPixels / widthDpi;
    float heightInches = heightPixels / heightDpi;

    double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));

    return diagonalInches;
}