I believe most people have read many articles about getting screen pixels when reading this article. From other articles, we know that the screen pixels are obtained based on the DisplayMetrics class.
The specific calculation method is width = widthPixels * density; height = heightPixels * density (ps: widthPixels and heightPixels, density are obtained from DisplayMetrics ).
Copy codeThe Code is as follows: DisplayMetrics dm = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics (dm );
Int widthPixels = dm. widthPixels;
Int heightPixels = dm. heightPixels;
Float density = dm. density;
Int screenWidth = widthPixels * density;
Int screenHeight = heightPixels * density;
The value of widthPixels ON phone 320*480 is 320, the value of heightPixels is 480, and the value of density is 1.0.
The value of idthPixels ON phone 480*800 is 320, the value of heightPixels is 533, and the value of density is 1.5.
Note: DisplayMetrics should not use context. getApplicationContext (). getResources (). getDisplayMetrics ();
I used this method to test in nexus one to get the density value of 1.0, so that I could not get the correct 480*800 pixels.