Sdílet prostřednictvím


Display.GetRealMetrics(DisplayMetrics) Method

Definition

Gets the size of the largest region of the display accessible to an app in the current system state, without subtracting any window decor or applying scaling factors.

[Android.Runtime.Register("getRealMetrics", "(Landroid/util/DisplayMetrics;)V", "GetGetRealMetrics_Landroid_util_DisplayMetrics_Handler")]
public virtual void GetRealMetrics (Android.Util.DisplayMetrics? outMetrics);
[<Android.Runtime.Register("getRealMetrics", "(Landroid/util/DisplayMetrics;)V", "GetGetRealMetrics_Landroid_util_DisplayMetrics_Handler")>]
abstract member GetRealMetrics : Android.Util.DisplayMetrics -> unit
override this.GetRealMetrics : Android.Util.DisplayMetrics -> unit

Parameters

outMetrics
DisplayMetrics

A DisplayMetrics object to receive the metrics.

Attributes

Remarks

Gets the size of the largest region of the display accessible to an app in the current system state, without subtracting any window decor or applying scaling factors.

The size is adjusted based on the current rotation of the display.

The returned size will fall into one of these scenarios: <ol> <li>The device has no partitions on the display. The returned value is the largest region of the display accessible to an app in the current system state, regardless of windowing mode.</li> <li>The device divides a single display into multiple partitions. An application is restricted to a portion of the display. This is common in devices where the display changes size, such as foldables or large screens. The returned size will match the portion of the display the application is restricted to.</li> <li>The window manager is emulating a different display size, using adb shell wm size. The returned size will match the emulated display size.</li> </ol> </p>

The returned value is <b>unsuitable to use when sizing and placing UI elements</b>, since it does not reflect the application window size in any of these scenarios. WindowManager#getCurrentWindowMetrics() is an alternative that returns the size of the current application window, even if the window is on a device with a partitioned display. This helps prevent UI bugs where UI elements are misaligned or placed beyond the bounds of the window.

Handling multi-window mode correctly is necessary since applications are not always fullscreen. A user on a large screen device, such as a tablet or ChromeOS devices, is more likely to use multi-window modes.

For example, consider a device with a display partitioned into two halves. The user may have a fullscreen application open on the first partition. They may have two applications open in split screen (an example of multi-window mode) on the second partition, with each application consuming half of the partition. In this case, WindowManager#getCurrentWindowMetrics() reports the fullscreen window is half of the screen in size, and each split screen window is a quarter of the screen in size. On the other hand, #getRealMetrics reports half of the screen size for all windows, since the application windows are all restricted to their respective partitions. </p>

This member is deprecated. Use WindowManager#getCurrentWindowMetrics() to identify the current size of the activity window. UI-related work, such as choosing UI layouts, should rely upon WindowMetrics#getBounds(). Use Configuration#densityDpi to get the current density.

Java documentation for android.view.Display.getRealMetrics(android.util.DisplayMetrics).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to