WindowMetrics
public
final
class
WindowMetrics
extends Object
java.lang.Object | |
↳ | android.view.WindowMetrics |
Metrics about a Window, consisting of the bounds and WindowInsets
.
This is usually obtained from WindowManager#getCurrentWindowMetrics()
and
WindowManager#getMaximumWindowMetrics()
.
Build.VERSION_CODES.UPSIDE_DOWN_CAKE
, it also provides density.
Obtains Window Dimensions in Density-independent Pixel(DP)
While getDensity()
is provided, the dimension in density-independent pixel could also be
calculated with WindowMetrics
properties, which is similar to
Configuration.screenWidthDp
float widthInDp = windowMetrics.getBounds().width() / windowMetrics.getDensity(); float heightInDp = windowMetrics.getBounds().height() / windowMetrics.getDensity();Also, the density in DPI can be obtained by:
float densityDp = DisplayMetrics.DENSITY_DEFAULT * windowMetrics.getDensity();
See also:
Summary
Public constructors | |
---|---|
WindowMetrics(Rect bounds, WindowInsets windowInsets)
This constructor is deprecated.
use |
|
WindowMetrics(Rect bounds, WindowInsets windowInsets, float density)
The constructor to create a |
Public methods | |
---|---|
Rect
|
getBounds()
Returns the bounds of the area associated with this window or |
float
|
getDensity()
Returns the density of the area associated with this window or |
WindowInsets
|
getWindowInsets()
Returns the |
String
|
toString()
Returns a string representation of the object. |
Inherited methods | |
---|---|
Public constructors
WindowMetrics
public WindowMetrics (Rect bounds, WindowInsets windowInsets)
This constructor is deprecated.
use WindowMetrics(android.graphics.Rect, android.view.WindowInsets, float)
instead.
Parameters | |
---|---|
bounds |
Rect : This value cannot be null . |
windowInsets |
WindowInsets : This value cannot be null . |
WindowMetrics
public WindowMetrics (Rect bounds, WindowInsets windowInsets, float density)
The constructor to create a WindowMetrics
instance.
Note that in most cases WindowMetrics
is obtained from
WindowManager#getCurrentWindowMetrics()
or
WindowManager#getMaximumWindowMetrics()
.
Parameters | |
---|---|
bounds |
Rect : The window bounds
This value cannot be null . |
windowInsets |
WindowInsets : The WindowInsets of the window
This value cannot be null . |
density |
float : The window density |
Public methods
getBounds
public Rect getBounds ()
Returns the bounds of the area associated with this window or UiContext
.
Note that the size of the reported bounds can have different size than
Display#getSize(Point)
based on your target API level and calling context.
This method reports the window size including all system
bar areas, while Display#getSize(Point)
can report the area excluding navigation bars
and display cutout areas depending on the calling context and target SDK level. Please refer
to Display#getSize(Point)
for details.
The value reported by Display#getSize(Point)
excluding system decoration areas can be
obtained by using:
final WindowMetrics metrics = windowManager.getCurrentWindowMetrics(); // Gets all excluding insets final WindowInsets windowInsets = metrics.getWindowInsets(); Insets insets = windowInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout()); int insetsWidth = insets.right + insets.left; int insetsHeight = insets.top + insets.bottom; // Legacy size that Display#getSize reports final Rect bounds = metrics.getBounds(); final Size legacySize = new Size(bounds.width() - insetsWidth, bounds.height() - insetsHeight);
Returns | |
---|---|
Rect |
window bounds in pixels.
This value cannot be null . |
getDensity
public float getDensity ()
Returns the density of the area associated with this window or UiContext
,
which uses the same units as DisplayMetrics.density
.
Returns | |
---|---|
float |
getWindowInsets
public WindowInsets getWindowInsets ()
Returns the WindowInsets
of the area associated with this window or
UiContext
.
Returns | |
---|---|
WindowInsets |
the WindowInsets of the visual area.
This value cannot be null . |
toString
public String toString ()
Returns a string representation of the object.
Returns | |
---|---|
String |
a string representation of the object. |