Android to get the screen longer than the width, referring to the online have a lot of code, but the results and the actual discrepancy, such as my phone is i9000, screen size is 480*800px, the result is 320*533 result is very unreliable, so I wrote a few lines of code, pro-Test Test parameters: Test environment: i9000 (Samsung) physical screen: 480*800pxdensity:1.5 //Get Screen Density (Method 1) int ScreenWidth = Getwindowmanager (). Getdefaultdisplay (). GetWidth (); Screen width (pixels, for example: 480px) int screenheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight (); Screen height (pixels, for example: 800p) log.e (TAG + " getdefaultdisplay", "screenwidth=" + ScreenWidth + "; screenheight= "+ screenheight); //Get screen Density (method 2) displaymetrics dm = new Displaymetrics (); &NBSP;&NBSP;&NBSP;DM = Getresources (). Getdisplaymetrics (); float density = dm.density; Screen density (pixel scale: 0.75/1.0/1.5/2.0) int densitydpi = dm.densitydpi; Screen density (pixels per inch: 120/160/240/320) float xdpi = dm.xdpi; float ydpi = dm.ydpi; &nbsP;&NBSP;&NBSP;&NBSP;LOG.E (TAG + " displaymetrics", "xdpi=" + xdpi + "; Ydpi= "+ ydpi", &NBSP;&NBSP;&NBSP;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) &NBSP;&NBSP;&NBSP;DM = new Displaymetrics (); getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); density = 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; &NBSP;&NBSP;&NBSP;LOG.E (TAG + " displaYmetrics "," xdpi= "+ xdpi +"; Ydpi= "+ ydpi", &NBSP;&NBSP;&NBSP;LOG.E (TAG + " displaymetrics", "density=" + density + "; Densitydpi= "+ densitydpi); int Screenwidthdip = Dm.widthpixels; Screen width (dip, for example: 320dip) int screenheightdip = 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); results as follows: e/== myscreenactivity =================================== Getdefaultdisplay (8509): screenwidth=320; screenheight=533e/== myscreenactivity =================================== displaymetrics (8509): xdpi=156.3077; ydpi=157.51938e/== myscreenactivity =================================== displaymetrics (8509): density=1.0; densitydpi=160e/== myscreenactivity =================================== displaymetrics (111) (8509): screenwidth=320; screenheight=533e/== myscreenactivity =================================== displaymetrics (8509): xdpi= 234.46153; ydpi=236.27907e/== myscreenactivity =================================== displaymetrics (8509): density=1.5; densitydpi=240e/== myscreenactivity =================================== displaymetrics (222) (8509): screenwidthdip=320; screenheightdip=533e/== myscreenactivity =================================== displaymetrics (222) (8509): screenwidth=480; screenheight=800 analysis results: In the OnDraw () Method 1 and 2, the results are consistent, are 320*533, obviously not the test machine i9000 screen Size Method 3, the method 1 and 2 results, multiplied by density, The Perfect 480*800,perfect! Note: Density greater than 1, you need to set targetsdkversion between 4-9, for example <USES-SDK android:minsdkversion= "3" Android:tarGetsdkversion= "/> " However, does this mean that method 3 must be generic? The answer is no, because I also tested on the simulator, HTC G14 physical machine, and ViewSonic, Galaxy tablet, Method 3, when density=1.5, magnified the actual screen value, for example: HTC G14 on HTC G14, the actual screen size, The actual physical screen size (540,960) is obtained directly through the Dm.widthpixels and Dm.heightpixels, which is why the real physical screen size cannot be obtained by a common method, possibly because the Android system is open source, Different handset manufacturers do not have a unified manufacturing standard to specify the mobile phone screen. Carefully analyze the code to find the problem in code: Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM) Initialize a Displaymetrics object from This display's data.dm = Getresources (). Getdisplaymetrics () Return The current display metrics that is in effect for this Resource object. The returned object should be treated as read-only. companion tour, a free dating site: www.jieberu.com
Push family, free tickets, scenic spots: www.tuituizu.com
Android get screen size and density (reprint)