New ways to get device resolution for Android Displaymetrics
Source: Internet
Author: User
<span id="Label3"></p><p style="text-indent: 2em"><p style="text-indent: 2em">Android phone resolution of a variety of, as a developer, the software can not only take into account one or several mobile phones, and in as many mobile phones as possible to achieve another satisfactory effect of our Reality. Android at the beginning of the design, the system was designed to run on a variety of different resolutions of the device operating System. For applications, the system platform provides them with a stable, cross-platform operating environment, and most of the technical details needed to display the program in the right way to the platform it runs are handled by the system itself, without the need for program Intervention. But after all, the effect is limited, and sometimes we need to optimize the program for the resolution of the DEVICE.</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">In the process of doing gobang, in order to make the layout more compatible, find some information about the resolution, the basic can be found on the internet is the use of displaymetrics to obtain the Resolution. After testing, we found that there is a way to obtain the resolution, which is to use the display class in the Android.view package.</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">The following displaymetrics of the relevant Information.</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Displaymetrics is a class in the Android.util package that provides general information about the display, such as display size, resolution, and Font.</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">To get the Displaymetrics member, first initialize an object as Follows:</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Displaymetricsmetrics dm = new Displaymetrics ();</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Getwindowmanager (). getdefaultdisplay (). getmetrics (dm);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">The constructor displaymetrics does not need to pass any arguments, and after calling Getwindowmanager (), the handle of the existing activity is obtained, at which point the Getdefaultdisplay () The Displaymetrics method stores the resulting width and height dimensions in the object, and the resulting width and height dimensions are in pixels (Pixel), and the pixels refer to absolute pixels rather than relative pixels. The implementation code is as Follows:</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Private TextView TextView;</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">public void OnCreate (Bundle Savedinstancestate) {</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Super.oncreate (savedinstancestate);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Setcontentview (r.layout.main);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Displaymetrics displaymetrics = new Displaymetrics ();</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Getwindowmanager (). getdefaultdisplay (). getmetrics (displaymetrics);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">String string = "phone screen Resolution is:" + Displaymetrics.widthpixels + "x" + displaymetrics.heightpixels;</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">TextView = (textView) Findviewbyid (r.id.textview);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Textview.settext (string);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">}</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Here is another way I found out, that is the display class, through the lookup api, this class in the Android.view package, described As: providesinformation about the display size and Density. Here's How to use It:</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Private TextView TextView;</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">public void OnCreate (Bundle Savedinstancestate) {</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Super.oncreate (savedinstancestate);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Setcontentview (r.layout.main);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Display display = Getwindowmanager (). getdefaultdisplay ();</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">String string = "the screen resolution of the phone is:" + display.getwidth () + "x"</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">+ Display.getheight ();</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">TextView = (textView) Findviewbyid (r.id.textview);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Textview.settext (string);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">}</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">The two methods run the same result: the display object can get the screen resolution not only by calling GetWidth () and GetHeight (), but also by Getmetrics () method to get the Displaymetrics object that describes this screen size and Resolution.</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">Displaymetrics displaymetrics= New Displaymetrics (); Display.getmetrics (displaymetrics);</p></p><p style="text-indent: 2em"><p style="text-indent: 2em">In addition, getorientation () can also get the orientation of the screen, getrefreshrate () get the screen refresh rate and so On.</p></p><p><p>New ways to get device resolution for Android Displaymetrics</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