Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.util;
     18 
     19 import android.os.SystemProperties;
     20 
     21 
     22 /**
     23  * A structure describing general information about a display, such as its
     24  * size, density, and font scaling.
     25  * <p>To access the DisplayMetrics members, initialize an object like this:</p>
     26  * <pre> DisplayMetrics metrics = new DisplayMetrics();
     27  * getWindowManager().getDefaultDisplay().getMetrics(metrics);</pre>
     28  */
     29 public class DisplayMetrics {
     30     /**
     31      * Standard quantized DPI for low-density screens.
     32      */
     33     public static final int DENSITY_LOW = 120;
     34 
     35     /**
     36      * Standard quantized DPI for medium-density screens.
     37      */
     38     public static final int DENSITY_MEDIUM = 160;
     39 
     40     /**
     41      * This is a secondary density, added for some common screen configurations.
     42      * It is recommended that applications not generally target this as a first
     43      * class density -- that is, don't supply specific graphics for this
     44      * density, instead allow the platform to scale from other densities
     45      * (typically {@link #DENSITY_HIGH}) as
     46      * appropriate.  In most cases (such as using bitmaps in
     47      * {@link android.graphics.drawable.Drawable}) the platform
     48      * can perform this scaling at load time, so the only cost is some slight
     49      * startup runtime overhead.
     50      *
     51      * <p>This density was original introduced to correspond with a
     52      * 720p TV screen: the density for 1080p televisions is
     53      * {@link #DENSITY_XHIGH}, and the value here provides the same UI
     54      * size for a TV running at 720p.  It has also found use in 7" tablets,
     55      * when these devices have 1280x720 displays.
     56      */
     57     public static final int DENSITY_TV = 213;
     58 
     59     /**
     60      * Standard quantized DPI for high-density screens.
     61      */
     62     public static final int DENSITY_HIGH = 240;
     63 
     64     /**
     65      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
     66      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
     67      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
     68      */
     69     public static final int DENSITY_260 = 260;
     70 
     71     /**
     72      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
     73      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
     74      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
     75      */
     76     public static final int DENSITY_280 = 280;
     77 
     78     /**
     79      * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
     80      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
     81      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
     82      */
     83     public static final int DENSITY_300 = 300;
     84 
     85     /**
     86      * Standard quantized DPI for extra-high-density screens.
     87      */
     88     public static final int DENSITY_XHIGH = 320;
     89 
     90     /**
     91      * Intermediate density for screens that sit somewhere between
     92      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
     93      * This is not a density that applications should target, instead relying
     94      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
     95      */
     96     public static final int DENSITY_340 = 340;
     97 
     98     /**
     99      * Intermediate density for screens that sit somewhere between
    100      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
    101      * This is not a density that applications should target, instead relying
    102      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
    103      */
    104     public static final int DENSITY_360 = 360;
    105 
    106     /**
    107      * Intermediate density for screens that sit somewhere between
    108      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
    109      * This is not a density that applications should target, instead relying
    110      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
    111      */
    112     public static final int DENSITY_400 = 400;
    113 
    114     /**
    115      * Intermediate density for screens that sit somewhere between
    116      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
    117      * This is not a density that applications should target, instead relying
    118      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
    119      */
    120     public static final int DENSITY_420 = 420;
    121 
    122     /**
    123      * Intermediate density for screens that sit somewhere between
    124      * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
    125      * This is not a density that applications should target, instead relying
    126      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
    127      */
    128     public static final int DENSITY_440 = 440;
    129 
    130     /**
    131      * Standard quantized DPI for extra-extra-high-density screens.
    132      */
    133     public static final int DENSITY_XXHIGH = 480;
    134 
    135     /**
    136      * Intermediate density for screens that sit somewhere between
    137      * {@link #DENSITY_XXHIGH} (480 dpi) and {@link #DENSITY_XXXHIGH} (640 dpi).
    138      * This is not a density that applications should target, instead relying
    139      * on the system to scale their {@link #DENSITY_XXXHIGH} assets for them.
    140      */
    141     public static final int DENSITY_560 = 560;
    142 
    143     /**
    144      * Standard quantized DPI for extra-extra-extra-high-density screens.  Applications
    145      * should not generally worry about this density; relying on XHIGH graphics
    146      * being scaled up to it should be sufficient for almost all cases.  A typical
    147      * use of this density would be 4K television screens -- 3840x2160, which
    148      * is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.
    149      */
    150     public static final int DENSITY_XXXHIGH = 640;
    151 
    152     /**
    153      * The reference density used throughout the system.
    154      */
    155     public static final int DENSITY_DEFAULT = DENSITY_MEDIUM;
    156 
    157     /**
    158      * Scaling factor to convert a density in DPI units to the density scale.
    159      * @hide
    160      */
    161     public static final float DENSITY_DEFAULT_SCALE = 1.0f / DENSITY_DEFAULT;
    162 
    163     /**
    164      * The device's current density.
    165      * <p>
    166      * This value reflects any changes made to the device density. To obtain
    167      * the device's stable density, use {@link #DENSITY_DEVICE_STABLE}.
    168      *
    169      * @hide This value should not be used.
    170      * @deprecated Use {@link #DENSITY_DEVICE_STABLE} to obtain the stable
    171      *             device density or {@link #densityDpi} to obtain the current
    172      *             density for a specific display.
    173      */
    174     @Deprecated
    175     public static int DENSITY_DEVICE = getDeviceDensity();
    176 
    177     /**
    178      * The device's stable density.
    179      * <p>
    180      * This value is constant at run time and may not reflect the current
    181      * display density. To obtain the current density for a specific display,
    182      * use {@link #densityDpi}.
    183      */
    184     public static final int DENSITY_DEVICE_STABLE = getDeviceDensity();
    185 
    186     /**
    187      * The absolute width of the available display size in pixels.
    188      */
    189     public int widthPixels;
    190     /**
    191      * The absolute height of the available display size in pixels.
    192      */
    193     public int heightPixels;
    194     /**
    195      * The logical density of the display.  This is a scaling factor for the
    196      * Density Independent Pixel unit, where one DIP is one pixel on an
    197      * approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen),
    198      * providing the baseline of the system's display. Thus on a 160dpi screen
    199      * this density value will be 1; on a 120 dpi screen it would be .75; etc.
    200      *
    201      * <p>This value does not exactly follow the real screen size (as given by
    202      * {@link #xdpi} and {@link #ydpi}, but rather is used to scale the size of
    203      * the overall UI in steps based on gross changes in the display dpi.  For
    204      * example, a 240x320 screen will have a density of 1 even if its width is
    205      * 1.8", 1.3", etc. However, if the screen resolution is increased to
    206      * 320x480 but the screen size remained 1.5"x2" then the density would be
    207      * increased (probably to 1.5).
    208      *
    209      * @see #DENSITY_DEFAULT
    210      */
    211     public float density;
    212     /**
    213      * The screen density expressed as dots-per-inch.  May be either
    214      * {@link #DENSITY_LOW}, {@link #DENSITY_MEDIUM}, or {@link #DENSITY_HIGH}.
    215      */
    216     public int densityDpi;
    217     /**
    218      * A scaling factor for fonts displayed on the display.  This is the same
    219      * as {@link #density}, except that it may be adjusted in smaller
    220      * increments at runtime based on a user preference for the font size.
    221      */
    222     public float scaledDensity;
    223     /**
    224      * The exact physical pixels per inch of the screen in the X dimension.
    225      */
    226     public float xdpi;
    227     /**
    228      * The exact physical pixels per inch of the screen in the Y dimension.
    229      */
    230     public float ydpi;
    231 
    232     /**
    233      * The reported display width prior to any compatibility mode scaling
    234      * being applied.
    235      * @hide
    236      */
    237     public int noncompatWidthPixels;
    238     /**
    239      * The reported display height prior to any compatibility mode scaling
    240      * being applied.
    241      * @hide
    242      */
    243     public int noncompatHeightPixels;
    244     /**
    245      * The reported display density prior to any compatibility mode scaling
    246      * being applied.
    247      * @hide
    248      */
    249     public float noncompatDensity;
    250     /**
    251      * The reported display density prior to any compatibility mode scaling
    252      * being applied.
    253      * @hide
    254      */
    255     public int noncompatDensityDpi;
    256     /**
    257      * The reported scaled density prior to any compatibility mode scaling
    258      * being applied.
    259      * @hide
    260      */
    261     public float noncompatScaledDensity;
    262     /**
    263      * The reported display xdpi prior to any compatibility mode scaling
    264      * being applied.
    265      * @hide
    266      */
    267     public float noncompatXdpi;
    268     /**
    269      * The reported display ydpi prior to any compatibility mode scaling
    270      * being applied.
    271      * @hide
    272      */
    273     public float noncompatYdpi;
    274 
    275     public DisplayMetrics() {
    276     }
    277 
    278     public void setTo(DisplayMetrics o) {
    279         if (this == o) {
    280             return;
    281         }
    282 
    283         widthPixels = o.widthPixels;
    284         heightPixels = o.heightPixels;
    285         density = o.density;
    286         densityDpi = o.densityDpi;
    287         scaledDensity = o.scaledDensity;
    288         xdpi = o.xdpi;
    289         ydpi = o.ydpi;
    290         noncompatWidthPixels = o.noncompatWidthPixels;
    291         noncompatHeightPixels = o.noncompatHeightPixels;
    292         noncompatDensity = o.noncompatDensity;
    293         noncompatDensityDpi = o.noncompatDensityDpi;
    294         noncompatScaledDensity = o.noncompatScaledDensity;
    295         noncompatXdpi = o.noncompatXdpi;
    296         noncompatYdpi = o.noncompatYdpi;
    297     }
    298 
    299     public void setToDefaults() {
    300         widthPixels = 0;
    301         heightPixels = 0;
    302         density =  DENSITY_DEVICE / (float) DENSITY_DEFAULT;
    303         densityDpi =  DENSITY_DEVICE;
    304         scaledDensity = density;
    305         xdpi = DENSITY_DEVICE;
    306         ydpi = DENSITY_DEVICE;
    307         noncompatWidthPixels = widthPixels;
    308         noncompatHeightPixels = heightPixels;
    309         noncompatDensity = density;
    310         noncompatDensityDpi = densityDpi;
    311         noncompatScaledDensity = scaledDensity;
    312         noncompatXdpi = xdpi;
    313         noncompatYdpi = ydpi;
    314     }
    315 
    316     @Override
    317     public boolean equals(Object o) {
    318         return o instanceof DisplayMetrics && equals((DisplayMetrics)o);
    319     }
    320 
    321     /**
    322      * Returns true if these display metrics equal the other display metrics.
    323      *
    324      * @param other The display metrics with which to compare.
    325      * @return True if the display metrics are equal.
    326      */
    327     public boolean equals(DisplayMetrics other) {
    328         return equalsPhysical(other)
    329                 && scaledDensity == other.scaledDensity
    330                 && noncompatScaledDensity == other.noncompatScaledDensity;
    331     }
    332 
    333     /**
    334      * Returns true if the physical aspects of the two display metrics
    335      * are equal.  This ignores the scaled density, which is a logical
    336      * attribute based on the current desired font size.
    337      *
    338      * @param other The display metrics with which to compare.
    339      * @return True if the display metrics are equal.
    340      * @hide
    341      */
    342     public boolean equalsPhysical(DisplayMetrics other) {
    343         return other != null
    344                 && widthPixels == other.widthPixels
    345                 && heightPixels == other.heightPixels
    346                 && density == other.density
    347                 && densityDpi == other.densityDpi
    348                 && xdpi == other.xdpi
    349                 && ydpi == other.ydpi
    350                 && noncompatWidthPixels == other.noncompatWidthPixels
    351                 && noncompatHeightPixels == other.noncompatHeightPixels
    352                 && noncompatDensity == other.noncompatDensity
    353                 && noncompatDensityDpi == other.noncompatDensityDpi
    354                 && noncompatXdpi == other.noncompatXdpi
    355                 && noncompatYdpi == other.noncompatYdpi;
    356     }
    357 
    358     @Override
    359     public int hashCode() {
    360         return widthPixels * heightPixels * densityDpi;
    361     }
    362 
    363     @Override
    364     public String toString() {
    365         return "DisplayMetrics{density=" + density + ", width=" + widthPixels +
    366             ", height=" + heightPixels + ", scaledDensity=" + scaledDensity +
    367             ", xdpi=" + xdpi + ", ydpi=" + ydpi + "}";
    368     }
    369 
    370     private static int getDeviceDensity() {
    371         // qemu.sf.lcd_density can be used to override ro.sf.lcd_density
    372         // when running in the emulator, allowing for dynamic configurations.
    373         // The reason for this is that ro.sf.lcd_density is write-once and is
    374         // set by the init process when it parses build.prop before anything else.
    375         return SystemProperties.getInt("qemu.sf.lcd_density",
    376                 SystemProperties.getInt("ro.sf.lcd_density", DENSITY_DEFAULT));
    377     }
    378 }
    379