Example usage for android.view Display getRealSize

List of usage examples for android.view Display getRealSize

Introduction

In this page you can find the example usage for android.view Display getRealSize.

Prototype

public void getRealSize(Point outSize) 

Source Link

Document

Gets the real size of the display without subtracting any window decor or applying any compatibility scale factors.

Usage

From source file:com.tencent.wetest.common.util.DeviceUtil.java

/**
 * ??/*  w  w w. jav a  2  s . c o m*/
 * @param cx 
 * @return ? ( width x height )
 */

public static String getDisplayMetrics(Context cx) {

    WindowManager mWindowManager = (WindowManager) cx.getSystemService(Context.WINDOW_SERVICE);
    Display display = mWindowManager.getDefaultDisplay();
    DisplayMetrics metric = new DisplayMetrics();

    Point size = new Point();

    String str = "";

    try {

        if (Build.VERSION.SDK_INT >= 11) {

            display.getRealSize(size);

            str += String.valueOf(size.x) + " x " + String.valueOf(size.y);

        } else {

            Method method = Class.forName("android.view.Display").getMethod("getRealMetrics",
                    DisplayMetrics.class);
            method.invoke(display, metric);

            str += String.valueOf(metric.widthPixels) + " x " + String.valueOf(metric.heightPixels);

        }

    } catch (Exception e) {

        display.getMetrics(metric);
        str += String.valueOf(metric.widthPixels) + " x " + String.valueOf(metric.heightPixels);
        Logger.error("getDeviceRealMetric Exception : " + e.toString());

    }

    return str;

}

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();
    DisplayMetrics metrics = new DisplayMetrics();
    d.getMetrics(metrics);//ww  w. j a v  a 2  s .com

    // 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:net.kevxu.muzei.interfacelift.InterfaceliftMacdropsClient.java

/**
 * Get suitable photo size based on the screen size of phone.
 *
 * @return Dimension Dimension of suitable photo size.
 *///from  w  w w  . j a va 2s.c o  m
protected Dimension getSuitablePhotoDimension() {
    WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    display.getRealSize(size);
    final int width = size.x;
    final int height = size.y;

    int screenLayout = mContext.getResources().getConfiguration().screenLayout;
    boolean isXlarge = ((screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);
    boolean isLarge = ((screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE);
    final boolean isTablet = isXlarge || isLarge;

    Dimension dimen;

    if (!isTablet) {
        // Wallpaper for phone needs at least [width x 2] x height
        dimen = new Dimension(width * 2, height);
    } else {
        // Wallpaper for tablet needs at least [long edge] x [long edge]
        int longEdge = width > height ? width : height;
        dimen = new Dimension(longEdge, longEdge);
    }

    return dimen;
}

From source file:processing.android.PFragment.java

public void initDimensions() {
    /*/*from   w  ww  . j  av a2 s.  c  o  m*/
    metrics = new DisplayMetrics();
    //    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    WindowManager wm = getActivity().getWindowManager();
    Display display = wm.getDefaultDisplay();
    display.getRealMetrics(metrics);
    */

    //    metrics = new DisplayMetrics();
    //    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    //    display.getRealMetrics(metrics); // API 17 or higher
    //    display.getRealSize(size);

    //    display.getMetrics(metrics);

    size = new Point();
    WindowManager wm = getActivity().getWindowManager();
    Display display = wm.getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealSize(size);
    } else if (Build.VERSION.SDK_INT >= 14) {
        // Use undocumented methods getRawWidth, getRawHeight
        try {
            size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        } catch (Exception e) {
            display.getSize(size);
        }
    }
}

From source file:org.xwalk.runtime.extension.api.device_capabilities.DeviceCapabilitiesDisplay.java

public JSONObject convertDisplayToJSON(Display disp) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    disp.getRealMetrics(displayMetrics);

    Point realSize = new Point();
    disp.getRealSize(realSize);

    Point availSize = new Point();
    disp.getSize(availSize);//from www.  j  a v a2 s.  c  om

    JSONObject out = new JSONObject();
    try {
        out.put("id", disp.getDisplayId());
        out.put("name", disp.getName());
        out.put("isPrimary", disp.getDisplayId() == disp.DEFAULT_DISPLAY);
        out.put("isInternal", disp.getDisplayId() == disp.DEFAULT_DISPLAY);
        out.put("dpiX", (int) displayMetrics.xdpi);
        out.put("dpiY", (int) displayMetrics.ydpi);
        out.put("width", realSize.x);
        out.put("height", realSize.y);
        out.put("availWidth", availSize.x);
        out.put("availHeight", availSize.y);
    } catch (JSONException e) {
        return mDeviceCapabilities.setErrorMessage(e.toString());
    }
    return out;
}

From source file:com.zeferino.bruno.pap.view.MovieDetailsSlidingTabLayout.java

/**
 * Create a default view to be used for tabs. This is called if a custom tab view is not set via
 * {@link #setCustomTabView(int, int)}./*from   ww w.ja v a 2  s .c  o  m*/
 */
protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    boolean phone = getResources().getBoolean(R.bool.portrait_only);
    Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getRealSize(size);
    int width = size.x;
    int height = size.y;
    // special case for 7' inch tablets
    // if we leave padding to 16 the text title on the viewPager will be cut
    if (!phone && ((width == 1024 && height == 600) || (height == 1024 && width == 600)))
        TAB_VIEW_PADDING_DIPS = 14;

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

From source file:android.support.v7.view.menu.MenuPopupHelper.java

/**
 * Creates the popup and assigns cached properties.
 *
 * @return an initialized popup//from  ww  w  .  j  ava  2s  . c  o  m
 */
@NonNull
private MenuPopup createPopup() {
    final WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    final Display display = windowManager.getDefaultDisplay();
    final Point displaySize = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealSize(displaySize);
    } else if (Build.VERSION.SDK_INT >= 13) {
        display.getSize(displaySize);
    } else {
        displaySize.set(display.getWidth(), display.getHeight());
    }

    final int smallestWidth = Math.min(displaySize.x, displaySize.y);
    final int minSmallestWidthCascading = mContext.getResources()
            .getDimensionPixelSize(R.dimen.abc_cascading_menus_min_smallest_width);
    final boolean enableCascadingSubmenus = smallestWidth >= minSmallestWidthCascading;

    final MenuPopup popup;
    if (enableCascadingSubmenus) {
        popup = new CascadingMenuPopup(mContext, mAnchorView, mPopupStyleAttr, mPopupStyleRes, mOverflowOnly);
    } else {
        popup = new StandardMenuPopup(mContext, mMenu, mAnchorView, mPopupStyleAttr, mPopupStyleRes,
                mOverflowOnly);
    }

    // Assign immutable properties.
    popup.addMenu(mMenu);
    popup.setOnDismissListener(mInternalOnDismissListener);

    // Assign mutable properties. These may be reassigned later.
    popup.setAnchorView(mAnchorView);
    popup.setCallback(mPresenterCallback);
    popup.setForceShowIcon(mForceShowIcon);
    popup.setGravity(mDropDownGravity);

    return popup;
}

From source file:org.navitproject.navit.Navit.java

public void fullscreen(int fullscreen) {
    int w, h;// w  w w. ja  va 2s.  c  om

    isFullscreen = (fullscreen != 0);
    if (isFullscreen) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    } else {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    Display display_ = getWindowManager().getDefaultDisplay();
    if (Build.VERSION.SDK_INT < 17) {
        w = display_.getWidth();
        h = display_.getHeight();
    } else {
        Point size = new Point();
        display_.getRealSize(size);
        w = size.x;
        h = size.y;
    }
    Log.d(TAG, String.format("Toggle fullscreen, w=%d, h=%d", w, h));
    N_NavitGraphics.handleResize(w, h);
}

From source file:com.htc.dotdesign.ToolBoxService.java

@Override
public void onCreate() {
    Log.d(DotDesignConstants.LOG_TAG, LOG_PREFIX + "onCreate");
    super.onCreate();

    Resources res = getResources();
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
    if (mLocalBroadcastManager != null) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(SHOW_HIDE_TOOL_BAR);
        filter.addAction(USER_START_DRAWING);
        mLocalBroadcastManager.registerReceiver(mToolBarReceiver, filter);
    }//from  ww  w.ja  v  a2  s  . c  o  m

    // Get full window size
    Display display = mWindowManager.getDefaultDisplay();
    Point size = new Point();
    display.getRealSize(size);
    mScreenWidth = size.x;
    mScreenHeight = size.y;

    // Get arrow width
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, R.drawable.dot_design_popupmenu_arrow, bitmapOptions);
    mArrowWidth = bitmapOptions.outWidth;

    // Initialize LayoutParams
    mDragButtonParams = new WindowManager.LayoutParams(res.getDimensionPixelSize(R.dimen.drag_button_width),
            res.getDimensionPixelSize(R.dimen.drag_button_height), WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
    mDragButtonParams.gravity = Gravity.TOP | Gravity.START;
    mDragButtonParams.x = 0;
    mDragButtonParams.y = mScreenHeight / 2;

    mToolBarParams = new WindowManager.LayoutParams(res.getDimensionPixelSize(R.dimen.tool_bar_width),
            res.getDimensionPixelSize(R.dimen.tool_bar_height), WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
    mToolBarParams.alpha = 0.9f;
    mToolBarParams.gravity = Gravity.TOP | Gravity.START;
    mToolBarParams.x = 0;
    mToolBarParams.y = mDragButtonParams.y;

    // Initialize drag button width and height
    //mDragButtonWidth = res.getDimensionPixelSize(R.dimen.drag_button_width);
    mDragButtonHeight = res.getDimensionPixelSize(R.dimen.drag_button_height);
    mDragBtnColorIcon = (GradientDrawable) res.getDrawable(R.drawable.round_button);
    mDragBtnEraserIcon = res.getDrawable(R.drawable.dot_design_circle_shape_dark);

    // Inflate layout
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mDragButton = inflater.inflate(R.layout.drag_button, null);
    mToolBarParent = inflater.inflate(R.layout.tool_bar, null);
    mPalette = inflater.inflate(R.layout.palette, null);
    mEraser = inflater.inflate(R.layout.eraser, null);
    mMenu = inflater.inflate(R.layout.menu, null);

    initToolBar();
    initMenu();
    initAnimation();
    initBrushColor();
    initDragButton();

    mWindowManager.addView(mToolBarParent, mToolBarParams);
    mWindowManager.addView(mDragButton, mDragButtonParams);
    mToolBarParent.setVisibility(View.INVISIBLE);
}

From source file:com.google.android.apps.santatracker.games.SplashActivity.java

@Override
protected void onStart() {
    super.onStart();

    // Orientation
    boolean gameIsLandscape = getIntent().getBooleanExtra(EXTRA_LANDSCAPE, false);
    boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int rotation = display.getRotation();

    // Figure out how many degrees to rotate
    // Landscape always wants to be at 90degrees, portrait always wants to be at 0degrees
    float degreesToRotate = 0f;
    if (rotation == Surface.ROTATION_0) {
        degreesToRotate = gameIsLandscape && !isLandscape ? 90.0f : 0.0f;
    } else if (rotation == Surface.ROTATION_90) {
        degreesToRotate = gameIsLandscape && isLandscape ? 0f : -90f;
    } else if (rotation == Surface.ROTATION_180) {
        degreesToRotate = gameIsLandscape && !isLandscape ? -90f : -180f;
    } else if (rotation == Surface.ROTATION_270) {
        degreesToRotate = gameIsLandscape && isLandscape ? -180f : -270f;
    }/* ww w .ja v  a 2 s. co m*/

    // On a TV, should always be 0
    if (isRunningOnTV()) {
        degreesToRotate = 0f;
    }

    // Rotate, if necessary
    if (degreesToRotate != 0) {
        Point size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            display.getRealSize(size);
        } else {
            display.getSize(size);
        }
        int w = size.x;
        int h = size.y;

        View mainLayout = findViewById(R.id.splash_layout);
        mainLayout.setRotation(degreesToRotate);
        mainLayout.setTranslationX((w - h) / 2);
        mainLayout.setTranslationY((h - w) / 2);

        ViewGroup.LayoutParams lp = mainLayout.getLayoutParams();
        lp.height = w;
        lp.width = h;

        mainLayout.requestLayout();
    }
}