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

/**
 * reference http://www.jianshu.com/p/1513134733d0
 *//* w w w  . j a v  a  2  s .com*/
public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera) {
    Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
        break;
    case Surface.ROTATION_90:
        degrees = 90;
        break;
    case Surface.ROTATION_180:
        degrees = 180;
        break;
    case Surface.ROTATION_270:
        degrees = 270;
        break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

From source file:Main.java

public static Bitmap captureWithStatusBar(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);/*from  w  w  w. j  ava  2 s.c  om*/
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, dm.widthPixels, dm.heightPixels);
    view.destroyDrawingCache();
    return ret;
}

From source file:com.hangulo.powercontact.util.Utils.java

public static int getScreenHeightSize(Activity activity) {
    Display display = activity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);//from  w w w.jav a  2  s .c  o m
    //int width = size.x;
    return (size.y);
}

From source file:com.hangulo.powercontact.util.Utils.java

public static int getScreenWidthtSize(Activity activity) {
    Display display = activity.getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);/*  www . j  ava 2 s .  c  om*/
    //int width = size.x;
    return (size.x);
}

From source file:Main.java

/**
 * Calculates the default orientation of the device.<br>
 * The default orientation of most phones is portrait, and the default orientation of most tablets is landscape.
 * //w w  w. ja  v a 2 s .com
 * @param activity Activity.
 * @return {@link Configuration#ORIENTATION_PORTRAIT} or {@link Configuration#ORIENTATION_LANDSCAPE}
 */
public static int getDefaultOrientationOfDevice(Activity activity) {
    Configuration configuration = activity.getResources().getConfiguration();
    boolean orientationLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);

    Display display = activity.getWindowManager().getDefaultDisplay();
    int rotation = display.getRotation();
    boolean parallelToDefaultVerticalAxis = (rotation == Surface.ROTATION_0)
            || (rotation == Surface.ROTATION_180);

    boolean defaultOrientationLandscape = (parallelToDefaultVerticalAxis && orientationLandscape)
            || (!parallelToDefaultVerticalAxis && !orientationLandscape);

    if (defaultOrientationLandscape) {
        return Configuration.ORIENTATION_LANDSCAPE;
    }
    return Configuration.ORIENTATION_PORTRAIT;
}

From source file:Main.java

public static int getDisplayWidth(Activity activity) {
    int width = 0;
    if (activity != null && activity.getWindowManager() != null
            && activity.getWindowManager().getDefaultDisplay() != null) {
        Point point = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(point);
        width = point.x;/*ww w. j  a  va  2s  .  c  om*/
    }
    return width;
}

From source file:Main.java

public static int getOrientationHint(Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;//w w w  . j  a  va2 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;
    }

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

From source file:Main.java

public static void setCameraDisplayOrientation(Activity activity, int cameraId,
        android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;//  w ww  .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 result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360; // compensate the mirror
    } else { // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }
    camera.setDisplayOrientation(result);
}

From source file:Main.java

public static BitmapDrawable writeOnDrawable(Activity actv, Resources res, int drawableId, String text,
        int textSize) {

    Bitmap bm = BitmapFactory.decodeResource(res, drawableId).copy(Bitmap.Config.ARGB_8888, true);

    DisplayMetrics dm = new DisplayMetrics();
    actv.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int pixelSize = (int) ((textSize * dm.scaledDensity));

    if (text.length() > 2) {
        pixelSize = (int) ((textSize * dm.scaledDensity) * (0.5 - (text.length() / 10)));
    }/*ww w .  j a va 2  s .c  om*/

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(pixelSize);
    paint.setTextAlign(Paint.Align.CENTER);

    // float adjust = paint.measureText(text);

    Canvas canvas = new Canvas(bm);
    int xPos = (int) ((bm.getWidth() / 2));
    int yPos = (int) ((bm.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(res, bm);
}

From source file:com.activiti.android.ui.utils.UIUtils.java

/**
 * Retrieve screen dimension./*from  w w  w  . ja  v  a  2s .c  o  m*/
 *
 * @param activity
 * @return
 */
public static int[] getScreenDimension(Activity activity) {
    int width = 0;
    int height = 0;

    Display display = activity.getWindowManager().getDefaultDisplay();
    if (AndroidVersion.isHCMR2OrAbove()) {
        Point size = new Point();
        display.getSize(size);
        width = size.x;
        height = size.y;
    } else {
        width = display.getWidth(); // deprecated
        height = display.getHeight(); // deprecated
    }

    return new int[] { width, height };
}