Android gets a wide screen height

Source: Internet
Author: User

get the width of the screen in the view constructor Public classGameview extends view{ PublicGameview (Context context) {Display D=( Activity) context). Getwindowmanager (). Getdefaultdisplay (); LOG.V (TAG,"W + H ="+d.getwidth () +" + "+d.getheight ()); }}==========================================================Displaymetrics defines some properties of the screen, you can get the displaymetrics properties of the current screen through the Getmetrics method, such as: Displaymetrics DM=NewDisplaymetrics ();    Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); intwidth = dm.widthpixels;//Get width    intHeight = dm.heightpixels;//Get High==========================================================The parameters passed in the OnLayout method in view can get the width of the current available zone:protected voidOnLayout (Boolean changed,intLeftintTopintRightintbottom) {    intwidth = right-left;//Get width    intHeight = bottom-top;//Get High}

Get Screen width height

//Get screen Width height (method 1)intScreenWidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth ();//screen width (pixels, for example: 480px)intScreenHeight = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();//screen height (pixels, for example: 800p)LOG.E (TAG +"Getdefaultdisplay","screenwidth="+ ScreenWidth +"; screenheight="+screenheight);//Get Screen Density (method 2)Displaymetrics DM =Newdisplaymetrics ();d m=getresources (). Getdisplaymetrics ();floatdensity = dm.density;//screen density (pixel scale: 0.75/1.0/1.5/2.0)intdensitydpi = dm.densitydpi;//screen density (pixels per inch: 120/160/240/320)floatXDPI =dm.xdpi;floatYDPI =dm.ydpi; LOG.E (TAG+"Displaymetrics","xdpi="+ xdpi +"; ydpi="+ydpi); LOG.E (TAG+"Displaymetrics","density="+ Density +"; densitydpi="+densitydpi); ScreenWidth= Dm.widthpixels;//screen width (pixels, for example: 480px)ScreenHeight = Dm.heightpixels;//screen height (pixels, for example: 800px)LOG.E (TAG +"Displaymetrics (111)","screenwidth="+ ScreenWidth +"; screenheight="+screenheight);//Get Screen Density (method 3)DM =Newdisplaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);d ensity= Dm.density;//screen density (pixel scale: 0.75/1.0/1.5/2.0)densitydpi = dm.densitydpi;//screen density (pixels per inch: 120/160/240/320)XDPI =dm.xdpi;ydpi=dm.ydpi; LOG.E (TAG+"Displaymetrics","xdpi="+ xdpi +"; ydpi="+ydpi); LOG.E (TAG+"Displaymetrics","density="+ Density +"; densitydpi="+densitydpi);intScreenwidthdip = Dm.widthpixels;//screen width (dip, for example: 320dip)intScreenheightdip = Dm.heightpixels;//screen width (dip, for example: 533dip)LOG.E (TAG +"displaymetrics (222)","screenwidthdip="+ Screenwidthdip +"; screenheightdip="+Screenheightdip); ScreenWidth= (int) (Dm.widthpixels * density +0.5f);//screen width (px, for example: 480px)ScreenHeight = (int) (Dm.heightpixels * density +0.5f);//screen height (px, for example: 800px)LOG.E (TAG +"displaymetrics (222)","screenwidth="+ ScreenWidth +"; screenheight="+ screenheight);

Gets the width of the control, in general, the width and height of the controls we get in OnCreate are all 0. Using the method below, you can get the true width and height

        //------------------------------------------------Method One        intW = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); intH = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED);        Imageview.measure (W, h); intHeight =imageview.getmeasuredheight (); intwidth =imageview.getmeasuredwidth (); Textview.append ("\ n"+height+","+width); This method will load onmeasure three times//-----------------------------------------------Method TwoViewtreeobserver vto =Imageview.getviewtreeobserver (); Vto.addonpredrawlistener (NewViewtreeobserver.onpredrawlistener () { PublicBoolean Onpredraw () {intHeight =imageview.getmeasuredheight (); intwidth =imageview.getmeasuredwidth (); Textview.append ("\ n"+height+","+width); return true;    }        }); This method will load onmeasure two times, but the callback function will callback many times//-----------------------------------------------Method ThreeViewtreeobserver Vto2 =Imageview.getviewtreeobserver (); Vto2.addongloballayoutlistener (NewOngloballayoutlistener () {@Override Public voidongloballayout () {imageview.getviewtreeobserver (). Removeglobalonlayoutlistener ( This); Textview.append ("\ n"+imageview.getheight () +","+imageview.getwidth ());      }          }); <pre name="Code" class="CSharp">
This method loads onmeasure two times, but the callback function only callbacks once

WindowManager WindowManager = Getwindowmanager ();         = Windowmanager.getdefaultdisplay ();         = display.getwidth ();        

Android gets a wide screen height

Related Article

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: 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.