Merge "Add density to WindowMetrics"
This commit is contained in:
@@ -52645,8 +52645,10 @@ package android.view {
|
||||
}
|
||||
|
||||
public final class WindowMetrics {
|
||||
ctor public WindowMetrics(@NonNull android.graphics.Rect, @NonNull android.view.WindowInsets);
|
||||
ctor @Deprecated public WindowMetrics(@NonNull android.graphics.Rect, @NonNull android.view.WindowInsets);
|
||||
ctor public WindowMetrics(@NonNull android.graphics.Rect, @NonNull android.view.WindowInsets, float);
|
||||
method @NonNull public android.graphics.Rect getBounds();
|
||||
method public float getDensity();
|
||||
method @NonNull public android.view.WindowInsets getWindowInsets();
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ import android.annotation.StyleableRes;
|
||||
import android.annotation.XmlRes;
|
||||
import android.app.Application;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.Context;
|
||||
import android.content.pm.ActivityInfo;
|
||||
import android.content.pm.ActivityInfo.Config;
|
||||
import android.content.res.loader.ResourcesLoader;
|
||||
@@ -2181,17 +2182,19 @@ public class Resources {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current display metrics that are in effect for this resource
|
||||
* Returns the current display metrics that are in effect for this resource
|
||||
* object. The returned object should be treated as read-only.
|
||||
*
|
||||
* <p>Note that the reported value may be different than the window this application is
|
||||
* interested in.</p>
|
||||
*
|
||||
* <p>Best practices are to obtain metrics from {@link WindowManager#getCurrentWindowMetrics()}
|
||||
* for window bounds, {@link Display#getRealMetrics(DisplayMetrics)} for display bounds and
|
||||
* obtain density from {@link Configuration#densityDpi}. The value obtained from this API may be
|
||||
* wrong if the {@link Resources} is from the context which is different than the window is
|
||||
* attached such as {@link Application#getResources()}.
|
||||
* <p>The best practices is to obtain metrics from
|
||||
* {@link WindowManager#getCurrentWindowMetrics()} for window bounds. The value obtained from
|
||||
* this API may be wrong if {@link Context#getResources()} is from
|
||||
* non-{@link android.annotation.UiContext}.
|
||||
* For example, use the {@link DisplayMetrics} obtained from {@link Application#getResources()}
|
||||
* to build {@link android.app.Activity} UI elements especially when the
|
||||
* {@link android.app.Activity} is in the multi-window mode or on the secondary {@link Display}.
|
||||
* <p/>
|
||||
*
|
||||
* @return The resource's current display metrics.
|
||||
|
||||
@@ -20,12 +20,23 @@ import android.annotation.Nullable;
|
||||
import android.compat.annotation.UnsupportedAppUsage;
|
||||
import android.content.res.FontScaleConverter;
|
||||
import android.os.SystemProperties;
|
||||
import android.view.WindowManager;
|
||||
|
||||
/**
|
||||
* A structure describing general information about a display, such as its
|
||||
* size, density, and font scaling.
|
||||
* <p>To access the DisplayMetrics members, retrieve display metrics like this:</p>
|
||||
* <pre>context.getResources().getDisplayMetrics();</pre>
|
||||
*
|
||||
* <p>
|
||||
* For UI layout, obtain {@link android.view.WindowMetrics} from
|
||||
* {@link WindowManager#getCurrentWindowMetrics()}. {@code DisplayMetrics} should only be used for
|
||||
* obtaining display related properties, such as {@link #xdpi} and {@link #ydpi}
|
||||
* </p><p>
|
||||
* See {@link #density} for more information about the differences between {@link #xdpi},
|
||||
* {@link #ydpi} and {@link #density}.
|
||||
* </p>
|
||||
*
|
||||
*/
|
||||
public class DisplayMetrics {
|
||||
/**
|
||||
|
||||
@@ -1469,7 +1469,8 @@ public final class Display {
|
||||
* @param outMetrics A {@link DisplayMetrics} object which receives the display metrics.
|
||||
*
|
||||
* @deprecated Use {@link WindowMetrics#getBounds()} to get the dimensions of the application
|
||||
* window. Use {@link Configuration#densityDpi} to get the display density.
|
||||
* window. Use {@link WindowMetrics#getDensity()} to get the density of the application
|
||||
* window.
|
||||
*/
|
||||
@Deprecated
|
||||
public void getMetrics(DisplayMetrics outMetrics) {
|
||||
|
||||
@@ -25,22 +25,64 @@ import android.graphics.Rect;
|
||||
* <p>
|
||||
* This is usually obtained from {@link WindowManager#getCurrentWindowMetrics()} and
|
||||
* {@link WindowManager#getMaximumWindowMetrics()}.
|
||||
* </p>
|
||||
* After {@link android.os.Build.VERSION_CODES#UPSIDE_DOWN_CAKE}, it also provides density.
|
||||
* <h3>Obtains Window Dimensions in Density-independent Pixel(DP)</h3>
|
||||
* <p>
|
||||
* While {@link #getDensity()} is provided, the dimension in density-independent pixel could also be
|
||||
* calculated with {@code WindowMetrics} properties, which is similar to
|
||||
* {@link android.content.res.Configuration#screenWidthDp}
|
||||
* <pre class="prettyprint">
|
||||
* float widthInDp = windowMetrics.getBounds().width() / windowMetrics.getDensity();
|
||||
* float heightInDp = windowMetrics.getBounds().height() / windowMetrics.getDensity();
|
||||
* </pre>
|
||||
* Also, the density in DPI can be obtained by:
|
||||
* <pre class="prettyprint">
|
||||
* float densityDp = DisplayMetrics.DENSITY_DEFAULT * windowMetrics.getDensity();
|
||||
* </pre>
|
||||
* </p>
|
||||
*
|
||||
* @see WindowInsets#getInsets(int)
|
||||
* @see WindowManager#getCurrentWindowMetrics()
|
||||
* @see WindowManager#getMaximumWindowMetrics()
|
||||
* @see android.annotation.UiContext
|
||||
*/
|
||||
public final class WindowMetrics {
|
||||
private final @NonNull Rect mBounds;
|
||||
private final @NonNull WindowInsets mWindowInsets;
|
||||
@NonNull
|
||||
private final Rect mBounds;
|
||||
@NonNull
|
||||
private final WindowInsets mWindowInsets;
|
||||
|
||||
/** @see android.util.DisplayMetrics#density */
|
||||
private final float mDensity;
|
||||
|
||||
/** @deprecated use {@link #WindowMetrics(Rect, WindowInsets, float)} instead. */
|
||||
@Deprecated
|
||||
public WindowMetrics(@NonNull Rect bounds, @NonNull WindowInsets windowInsets) {
|
||||
mBounds = bounds;
|
||||
mWindowInsets = windowInsets;
|
||||
this(bounds, windowInsets, 1.0f);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bounds of the area associated with this window or visual context.
|
||||
* The constructor to create a {@link WindowMetrics} instance.
|
||||
* <p>
|
||||
* Note that in most cases {@link WindowMetrics} is obtained from
|
||||
* {@link WindowManager#getCurrentWindowMetrics()} or
|
||||
* {@link WindowManager#getMaximumWindowMetrics()}.
|
||||
* </p>
|
||||
*
|
||||
* @param bounds The window bounds
|
||||
* @param windowInsets The {@link WindowInsets} of the window
|
||||
* @param density The window density
|
||||
*/
|
||||
public WindowMetrics(@NonNull Rect bounds, @NonNull WindowInsets windowInsets, float density) {
|
||||
mBounds = bounds;
|
||||
mWindowInsets = windowInsets;
|
||||
mDensity = density;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the bounds of the area associated with this window or
|
||||
* {@link android.annotation.UiContext}.
|
||||
* <p>
|
||||
* <b>Note that the size of the reported bounds can have different size than
|
||||
* {@link Display#getSize(Point)}.</b> This method reports the window size including all system
|
||||
@@ -66,16 +108,40 @@ public final class WindowMetrics {
|
||||
*
|
||||
* @return window bounds in pixels.
|
||||
*/
|
||||
public @NonNull Rect getBounds() {
|
||||
@NonNull
|
||||
public Rect getBounds() {
|
||||
return mBounds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link WindowInsets} of the area associated with this window or visual context.
|
||||
* Returns the {@link WindowInsets} of the area associated with this window or
|
||||
* {@link android.annotation.UiContext}.
|
||||
*
|
||||
* @return the {@link WindowInsets} of the visual area.
|
||||
*/
|
||||
public @NonNull WindowInsets getWindowInsets() {
|
||||
@NonNull
|
||||
public WindowInsets getWindowInsets() {
|
||||
return mWindowInsets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the density of the area associated with this window or
|
||||
* {@link android.annotation.UiContext}, which uses the same units as
|
||||
* {@link android.util.DisplayMetrics#density}.
|
||||
*
|
||||
* @see android.util.DisplayMetrics#DENSITY_DEFAULT
|
||||
* @see android.util.DisplayMetrics#density
|
||||
*/
|
||||
public float getDensity() {
|
||||
return mDensity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return WindowMetrics.class.getSimpleName() + ":{"
|
||||
+ "bounds=" + mBounds
|
||||
+ ", windowInsets=" + mWindowInsets
|
||||
+ ", density" + mDensity
|
||||
+ "}";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user