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