Get Phone Screen Displaymetrics property method

Source: Internet
Author: User

First of all, there is a point to declare that, in fact, in Android, the canvas is the height of the width of the screen is actually high.

As a result, there are many ways to get a high screen width:

1.

Java code

WindowManager WindowManager = Getwindowmanager ();

Display display = Windowmanager.getdefaultdisplay ();

ScreenWidth = Display.getwidth ();

ScreenHeight = Display.getheight ();



2.

Java code

Displaymetrics DM = new displaymetrics ();

this. Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);//this refers to the current activity

ScreenWidth =dm.widthpixels;

ScreenHeight =dm.heightpixels;

Both of these methods are still in the 0 state when the screen is not displayed, that is, after the Setcontentview call is not valid.

3.

It can also be obtained by canvas in OnDraw.

Java Code screenwidth =canvas.getwidth (); ScreenHeight =canvas.getheight ();



And the view of the aspect is what most people think of the canvas of the high-width, in fact, he refers to the taskbar and the status bar, in addition to the high-width, mainly the following methods:

1.

Java code

Viewwidth=this. getwidth ();

viewheight=this. getheight ();



2. Rewrite the view's onsizechanged, the method will be called after OnCreate, OnDraw before the

Java code protected voidOnsizechanged ( intW intH intOLDW, intOLDH) {viewheight=h; Viewwidth=w; Super. onsizechanged (W, H, OLDW, OLDH); }

High and wide width of taskbar, status bar I'm not sure yet, but looking from the Internet:

Reference Frameworks/core/res/values/themes.xml
<item name= "Windowtitlesize" >25dip</item>
Frameworks/core/res/values/dimens.xml
<dimen name= "Status_bar_height" >25dip</dimen>

If you want to get a high width in an activity, it's best to put it in the onsizechanged for more insurance.

The above is a summary of my high-width Android, if there are errors in the place, welcome to discuss the message.

In addition, there are some settings for the screen display:

Set to Untitled

Java code requestwindowfeature (window.feature_no_title);



Set to full-screen mode GetWindow (). setflags

Java code

GetWindow (). SetFlags (Windowmanager.layoutparams.flag_fullscreen,windowmanager.layoutparams.flag_fullscreen);



Set to horizontal screen

Java code

Setrequesteorientation (Activityinfo.screen_orientation_ladscape);

(Transferred from: http://blog.csdn.net/yiyaaixuexi/article/details/6233005)

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.