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 ()); } });
This method loads onmeasure two times, but the callback function only callbacks once
Android Get screen width height with Get control wide height