diff options
| author | Charles Chen <charlesccchen@google.com> | 2020-03-02 19:00:37 +0800 |
|---|---|---|
| committer | Charles Chen <charlesccchen@google.com> | 2020-04-01 18:29:51 +0800 |
| commit | 529318a713fc416b39fd67a4e6b6c8475ae16760 (patch) | |
| tree | 34797f57f36e16ef22b909d58153f2fff419ecbc /core/java | |
| parent | 884aa6afe156ec88cfc89e9d5dac4480223c9495 (diff) | |
Update documentation of Resources#getDisplayMetrics
Test: build & run
fixes: 150213575
Change-Id: I439a8f8473236e29cc6371cda51e46a3b242ec3e
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/content/res/Resources.java | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index d6a9f6990abe..c399bc72e438 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -40,6 +40,7 @@ import android.annotation.StringRes; import android.annotation.StyleRes; import android.annotation.StyleableRes; import android.annotation.XmlRes; +import android.app.Application; import android.compat.annotation.UnsupportedAppUsage; import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo.Config; @@ -58,9 +59,11 @@ import android.util.Log; import android.util.LongSparseArray; import android.util.Pools.SynchronizedPool; import android.util.TypedValue; +import android.view.Display; import android.view.DisplayAdjustments; import android.view.ViewDebug; import android.view.ViewHierarchyEncoder; +import android.view.WindowManager; import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.VisibleForTesting; @@ -101,6 +104,12 @@ import java.util.List; * (such as for different languages and screen sizes). This is an important aspect of developing * Android applications that are compatible on different types of devices.</p> * + * <p>After {@link Build.VERSION_CODES#R}, {@link Resources} must be obtained by + * {@link android.app.Activity} or {@link android.content.Context} created with + * {@link android.content.Context#createWindowContext(int, Bundle)}. + * {@link Application#getResources()} may report wrong values in multi-window or on secondary + * displays. + * * <p>For more information about using resources, see the documentation about <a * href="{@docRoot}guide/topics/resources/index.html">Application Resources</a>.</p> */ @@ -2024,10 +2033,20 @@ public class Resources { } /** - * Return the current display metrics that are in effect for this resource - * object. The returned object should be treated as read-only. - * - * @return The resource's current display metrics. + * Return 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/> + * + * @return The resource's current display metrics. */ public DisplayMetrics getDisplayMetrics() { return mResourcesImpl.getDisplayMetrics(); |
