<span id="Label3"></p>In the app code often need to get the screen resolution of the phone (wide * high), originally I directly online copy code, but in the use of the process found a lot of inconvenience.<br><p><p>Inconvenience one: getwidth and getheight in the code below indicate that deprecated has been abandoned on adt, which is a glaring</p></p><p><p></p></p><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_1_4652903" name="code" class="html"><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_1_4652903" name="code" class="html">WindowManager wm = Getwindowmanager (); Display display = Wm.getdefaultdisplay (); int screenwidth = Display.getwidth (); </pre></pre><br><br>View Android source says you can use GetSize instead, then replace it with GetSize and extract the code into the tool class<p><p></p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_2_7673355" name="code" class="html"><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_2_7673355" name="code" class="html"> public static point GetSize (Activity Act) { Display display = Act.getwindowmanager (). getdefaultdisplay (); Point size = new Point (); Display.getsize (size); Size.x is the width, size.y is the height return size; } </pre></pre><br>A burst of discovery and inconvenience, because the Getwindowmanager method can only be used in activity or called by the activity instance, as we want to get the screen resolution in fragment or adapter, There are also difficulties (because fragment and adapter generally only the context), although can be forced type conversion to deal with, but after all, costly need to remember to Convert. So changed from the context to get the system service, and then to get the resolution, the code is as follows<p><p></p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_3_9755715" name="code" class="html"><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_3_9755715" name="code" class="html"> public static point GetSize (Context ctx) { windowmanager wm = (windowmanager) Ctx.getsystemservice (context.window_ SERVICE); Display display = Wm.getdefaultdisplay (); Point size = new Point (); Display.getsize (size); return size; } </pre></pre><br>Later found through Displaymetrics can also obtain resolution, according to the online practice tiger as Follows:<p><p></p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_4_5741771" name="code" class="html"><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_4_5741771" name="code" class="html"> public static point getsizenew (Context ctx) { windowmanager wm = (windowmanager) Ctx.getsystemservice ( context.window_service); Displaymetrics dm = new Displaymetrics (); Wm.getdefaultdisplay (). getmetrics (dm); Point size = new Point (); size.x = dm.widthpixels*dm.density; Size.y = dm.heightpixels*dm.density; return size; } </pre></pre><br>Do not handy found this new method is often wrong, find to go and their own repeated practice, the results confirm that widthpixels and heightpixels is wide and high, no need to multiply on the Density. Because Widthpixels and heightpixels units are pixels, and density refers to pixel density, that is, there are several pixels in a unit, so it doesn't make sense to multiply density on my side, only by dividing Density. Dm.widthpixels/dm.densit refers to the width of how many units, dm.heightpixels/dm.density refers to the height of how many Units. Density on the internet, it may just be that they happen to meet some special aircraft. The final code is as follows<p><p></p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_5_1237459" name="code" class="html"><pre code_snippet_id="1809384" snippet_file_name="blog_20160804_5_1237459" name="code" class="html"> public static point getsizenew (Context ctx) { windowmanager wm = (windowmanager) Ctx.getsystemservice ( context.window_service); Displaymetrics dm = new Displaymetrics (); Wm.getdefaultdisplay (). getmetrics (dm); Point size = new Point (); size.x = dm.widthpixels; Size.y = dm.heightpixels; return size; } </pre></pre><br><br><p><p></p></p><p><p>Android Beginner Tutorial: Screen resolution</p></p></span>
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