[Java] // The first method returns pixels.
Display display = getWindowManager (). getDefaultDisplay ();
Log. I ("MainActivity", "height:" + display. getHeight ());
Log. I ("MainActivity", "width:" + display. getWidth ());
// The second method returns pixels.
DisplayMetrics displayMetrics = getResources (). getDisplayMetrics ();
Log. I ("MainActivity", "height" + displayMetrics. heightPixels );
Log. I ("MainActivity", "width" + displayMetrics. widthPixels );
// The first method returns pixels.
Display display = getWindowManager (). getDefaultDisplay ();
Log. I ("MainActivity", "height:" + display. getHeight ());
Log. I ("MainActivity", "width:" + display. getWidth ());
// The second method returns pixels.
DisplayMetrics displayMetrics = getResources (). getDisplayMetrics ();
Log. I ("MainActivity", "height" + displayMetrics. heightPixels );
Log. I ("MainActivity", "width" + displayMetrics. widthPixels );
In fact, in general, you do not need to know the width or height of the mobile phone. if you know the height of the mobile phone, and then make various if else or swith judgments, this code is very bad. In fact, at the beginning of Android design, the resolution of different devices is different. How can this problem be solved? Apart from distinguishing image folders like drawable-hdpi and drawable-ldpi, you can also create values-hdpi and values-ldpi, or create a folder Based on the width and height of a specific device, for example, values-800x480 and values-960x540 in. Note: x is a lowercase letter x, not a multiplication number, and must be preceded by a large number.
The csdn image is added with a watermark...