Old Deng's android Study Notes (2)-Android gets screen resolutions in N ways, android-android

Source: Internet
Author: User

Old Deng's android Study Notes (2)-Android gets screen resolutions in N ways, android-android

Android gets the screen resolution

Method 1: the simplest method

12 int screenWidth  = getWindowManager().getDefaultDisplay().getWidth();  int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); 


It can be done in two simple sentences. Try to use this method for testing first. If this method cannot pass the test, replace the following method.

Method 2: obtain a general algorithm for screen size comparison

12345678 DisplayMetrics dm = new DisplayMetrics();  dm = getResources().getDisplayMetrics();  float density  = dm.density;        int densityDPI = dm.densityDpi;  float xdpi = dm.xdpi;             float ydpi = dm.ydpi;  screenWidth  = dm.widthPixels;  screenHeight = dm.heightPixels;  

This method supports most mobile phones. In general, if the above method fails, you can test it by modifying the method. It is best to do this...

Method 3:

12345678910 dm = new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(dm);  density  = dm.density;  densityDPI = dm.densityDpi; xdpi = dm.xdpi;           ydpi = dm.ydpi;  int screenWidthDip = dm.widthPixels; int screenHeightDip = dm.heightPixels;  screenWidth  = (int)(dm.widthPixels * density + 0.5f);  screenHeight = (int)(dm.heightPixels * density + 0.5f); 

 

This method is suitable for special models, such as i9000. It can be used for good identification and verification. The principle is to multiply the result of method 1 and method 2 by the density (when the density is greater than 1, The targetSdkVersion must be set to 4-9)

 

Note: currently, the fragmentation of android is very serious, and many methods cannot be used.


Address: http://www.itmmd.com/201411/161.html
This article is organized and published by Meng IT personnel. The reprinted article must indicate the source.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.