Android gets screen height and width (using the GetSize (point) method in display)

Source: Internet
Author: User

Method One:

public static int screen_width;
public static int screen_height;

Get screen
WindowManager wm = (WindowManager) getbasecontext (). Getsystemservice (
Context.window_service);
Display display = Wm.getdefaultdisplay ();
Point size = new Point ();
Display.getsize (size);
Screen_width= size.x;
Screen_height= size.y;

Display in the GetSize (point) method uses:

Display display = Getwindowmanager (). Getdefaultdisplay ();

int width = display.getwidth (); Deprecated

int height = display.getheight (); Deprecated

View API Discovery:

int getwidth (), int getheight () method: (I'm using level 19)

This method is deprecated in API level 13. Use getSize(Point) instead.

Take a look at the GetSize (point) method:

public void GetSize (point outsize)

Added in API level 13

Gets the size of the display, in pixels.

Note that this value should is used for computing layouts, since a device would typically has screen Decorati On (such as a status bar) along the edges of the display, the amount of application space available from the SI Ze returned here. Layouts should instead use the window size.

The size is adjusted based on the current rotation of the display.

The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display. The returned size is adjusted to exclude certain system decoration elements, that is always visible. It may also is scaled to provide compatibility with older applications, were originally designed for smaller displays

Parameters

Outsize A Point object to receive the size information.
So how to use it: Scenario 1 : If You want the the display dimensions in pixels you can usegetsize:Display display = Getwindowmanager (). Getdefaultdisplay ();
Point size = new Point ();
Display.getsize (size);
int width = size.x;
int height = size.y; Scenario 2:if you ' re not in a Activity you can get the default Display Viawindow_service:display = ((WindowManager) context.gets Ystemservice (context. Window_service)). Getdefaultdisplay ();
Diswidth = GetContext (). Getresources (). Getdisplaymetrics (). Widthpixels;
Disheight = GetContext (). Getresources (). Getdisplaymetrics (). Heightpixels;

Android gets screen height and width (using the GetSize (point) method in display)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.