Class DisplayMetrics
- java.lang.Object
-
- android.util.DisplayMetrics
-
public class DisplayMetrics extends Object
A structure describing general information about a display, such as its size, density, and font scaling.To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);
-
-
Field Summary
Fields Modifier and Type Field Description floatdensityThe logical density of the display.static intDENSITY_280Intermediate density for screens that sit betweenDENSITY_HIGH(240dpi) andDENSITY_XHIGH(320dpi).static intDENSITY_360Intermediate density for screens that sit somewhere betweenDENSITY_XHIGH(320 dpi) andDENSITY_XXHIGH(480 dpi).static intDENSITY_400Intermediate density for screens that sit somewhere betweenDENSITY_XHIGH(320 dpi) andDENSITY_XXHIGH(480 dpi).static intDENSITY_420Intermediate density for screens that sit somewhere betweenDENSITY_XHIGH(320 dpi) andDENSITY_XXHIGH(480 dpi).static intDENSITY_560Intermediate density for screens that sit somewhere betweenDENSITY_XXHIGH(480 dpi) andDENSITY_XXXHIGH(640 dpi).static intDENSITY_DEFAULTThe reference density used throughout the system.static floatDENSITY_DEFAULT_SCALEScaling factor to convert a density in DPI units to the density scale.static intDENSITY_DEVICEDeprecated.There is no longer a static density; you can find the density for a display indensityDpi.static intDENSITY_HIGHStandard quantized DPI for high-density screens.static intDENSITY_LOWStandard quantized DPI for low-density screens.static intDENSITY_MEDIUMStandard quantized DPI for medium-density screens.static intDENSITY_TVThis is a secondary density, added for some common screen configurations.static intDENSITY_XHIGHStandard quantized DPI for extra-high-density screens.static intDENSITY_XXHIGHStandard quantized DPI for extra-extra-high-density screens.static intDENSITY_XXXHIGHStandard quantized DPI for extra-extra-extra-high-density screens.intdensityDpiThe screen density expressed as dots-per-inch.intheightPixelsThe absolute height of the display in pixels.floatnoncompatDensityThe reported display density prior to any compatibility mode scaling being applied.intnoncompatDensityDpiThe reported display density prior to any compatibility mode scaling being applied.intnoncompatHeightPixelsThe reported display height prior to any compatibility mode scaling being applied.floatnoncompatScaledDensityThe reported scaled density prior to any compatibility mode scaling being applied.intnoncompatWidthPixelsThe reported display width prior to any compatibility mode scaling being applied.floatnoncompatXdpiThe reported display xdpi prior to any compatibility mode scaling being applied.floatnoncompatYdpiThe reported display ydpi prior to any compatibility mode scaling being applied.floatscaledDensityA scaling factor for fonts displayed on the display.intwidthPixelsThe absolute width of the display in pixels.floatxdpiThe exact physical pixels per inch of the screen in the X dimension.floatydpiThe exact physical pixels per inch of the screen in the Y dimension.
-
Constructor Summary
Constructors Constructor Description DisplayMetrics()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(DisplayMetrics other)Returns true if these display metrics equal the other display metrics.booleanequals(Object o)Compares this instance with the specified object and indicates if they are equal.booleanequalsPhysical(DisplayMetrics other)Returns true if the physical aspects of the two display metrics are equal.inthashCode()Returns an integer hash code for this object.voidsetTo(DisplayMetrics o)voidsetToDefaults()StringtoString()Returns a string containing a concise, human-readable description of this object.
-
-
-
Field Detail
-
DENSITY_LOW
public static final int DENSITY_LOW
Standard quantized DPI for low-density screens.- See Also:
- Constant Field Values
-
DENSITY_MEDIUM
public static final int DENSITY_MEDIUM
Standard quantized DPI for medium-density screens.- See Also:
- Constant Field Values
-
DENSITY_TV
public static final int DENSITY_TV
This is a secondary density, added for some common screen configurations. It is recommended that applications not generally target this as a first class density -- that is, don't supply specific graphics for this density, instead allow the platform to scale from other densities (typicallyDENSITY_HIGH) as appropriate. In most cases (such as using bitmaps inandroid.graphics.drawable.Drawable) the platform can perform this scaling at load time, so the only cost is some slight startup runtime overhead.This density was original introduced to correspond with a 720p TV screen: the density for 1080p televisions is
DENSITY_XHIGH, and the value here provides the same UI size for a TV running at 720p. It has also found use in 7" tablets, when these devices have 1280x720 displays.- See Also:
- Constant Field Values
-
DENSITY_HIGH
public static final int DENSITY_HIGH
Standard quantized DPI for high-density screens.- See Also:
- Constant Field Values
-
DENSITY_280
public static final int DENSITY_280
Intermediate density for screens that sit betweenDENSITY_HIGH(240dpi) andDENSITY_XHIGH(320dpi). This is not a density that applications should target, instead relying on the system to scale theirDENSITY_XHIGHassets for them.- See Also:
- Constant Field Values
-
DENSITY_XHIGH
public static final int DENSITY_XHIGH
Standard quantized DPI for extra-high-density screens.- See Also:
- Constant Field Values
-
DENSITY_360
public static final int DENSITY_360
Intermediate density for screens that sit somewhere betweenDENSITY_XHIGH(320 dpi) andDENSITY_XXHIGH(480 dpi). This is not a density that applications should target, instead relying on the system to scale theirDENSITY_XXHIGHassets for them.- See Also:
- Constant Field Values
-
DENSITY_400
public static final int DENSITY_400
Intermediate density for screens that sit somewhere betweenDENSITY_XHIGH(320 dpi) andDENSITY_XXHIGH(480 dpi). This is not a density that applications should target, instead relying on the system to scale theirDENSITY_XXHIGHassets for them.- See Also:
- Constant Field Values
-
DENSITY_420
public static final int DENSITY_420
Intermediate density for screens that sit somewhere betweenDENSITY_XHIGH(320 dpi) andDENSITY_XXHIGH(480 dpi). This is not a density that applications should target, instead relying on the system to scale theirDENSITY_XXHIGHassets for them.- See Also:
- Constant Field Values
-
DENSITY_XXHIGH
public static final int DENSITY_XXHIGH
Standard quantized DPI for extra-extra-high-density screens.- See Also:
- Constant Field Values
-
DENSITY_560
public static final int DENSITY_560
Intermediate density for screens that sit somewhere betweenDENSITY_XXHIGH(480 dpi) andDENSITY_XXXHIGH(640 dpi). This is not a density that applications should target, instead relying on the system to scale theirDENSITY_XXXHIGHassets for them.- See Also:
- Constant Field Values
-
DENSITY_XXXHIGH
public static final int DENSITY_XXXHIGH
Standard quantized DPI for extra-extra-extra-high-density screens. Applications should not generally worry about this density; relying on XHIGH graphics being scaled up to it should be sufficient for almost all cases. A typical use of this density would be 4K television screens -- 3840x2160, which is 2x a traditional HD 1920x1080 screen which runs at DENSITY_XHIGH.- See Also:
- Constant Field Values
-
DENSITY_DEFAULT
public static final int DENSITY_DEFAULT
The reference density used throughout the system.- See Also:
- Constant Field Values
-
DENSITY_DEFAULT_SCALE
public static final float DENSITY_DEFAULT_SCALE
Scaling factor to convert a density in DPI units to the density scale.- See Also:
- Constant Field Values
-
DENSITY_DEVICE
@Deprecated public static int DENSITY_DEVICE
Deprecated.There is no longer a static density; you can find the density for a display indensityDpi.The device's density.
-
widthPixels
public int widthPixels
The absolute width of the display in pixels.
-
heightPixels
public int heightPixels
The absolute height of the display in pixels.
-
density
public float density
The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.This value does not exactly follow the real screen size (as given by
xdpiandydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).- See Also:
DENSITY_DEFAULT
-
densityDpi
public int densityDpi
The screen density expressed as dots-per-inch. May be eitherDENSITY_LOW,DENSITY_MEDIUM, orDENSITY_HIGH.
-
scaledDensity
public float scaledDensity
A scaling factor for fonts displayed on the display. This is the same asdensity, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.
-
xdpi
public float xdpi
The exact physical pixels per inch of the screen in the X dimension.
-
ydpi
public float ydpi
The exact physical pixels per inch of the screen in the Y dimension.
-
noncompatWidthPixels
public int noncompatWidthPixels
The reported display width prior to any compatibility mode scaling being applied.
-
noncompatHeightPixels
public int noncompatHeightPixels
The reported display height prior to any compatibility mode scaling being applied.
-
noncompatDensity
public float noncompatDensity
The reported display density prior to any compatibility mode scaling being applied.
-
noncompatDensityDpi
public int noncompatDensityDpi
The reported display density prior to any compatibility mode scaling being applied.
-
noncompatScaledDensity
public float noncompatScaledDensity
The reported scaled density prior to any compatibility mode scaling being applied.
-
noncompatXdpi
public float noncompatXdpi
The reported display xdpi prior to any compatibility mode scaling being applied.
-
noncompatYdpi
public float noncompatYdpi
The reported display ydpi prior to any compatibility mode scaling being applied.
-
-
Method Detail
-
setTo
public void setTo(DisplayMetrics o)
-
setToDefaults
public void setToDefaults()
-
equals
public boolean equals(Object o)
Description copied from class:ObjectCompares this instance with the specified object and indicates if they are equal. In order to be equal,omust represent the same object as this instance using a class-specific comparison. The general contract is that this comparison should be reflexive, symmetric, and transitive. Also, no object reference other than null is equal to null.The default implementation returns
trueonly ifthis == o. See Writing a correctequalsmethod if you intend implementing your ownequalsmethod.The general contract for the
equalsandObject.hashCode()methods is that ifequalsreturnstruefor any two objects, thenhashCode()must return the same value for these objects. This means that subclasses ofObjectusually override either both methods or neither of them.- Overrides:
equalsin classObject- Parameters:
o- the object to compare this instance with.- Returns:
trueif the specified object is equal to thisObject;falseotherwise.- See Also:
Object.hashCode()
-
equals
public boolean equals(DisplayMetrics other)
Returns true if these display metrics equal the other display metrics.- Parameters:
other- The display metrics with which to compare.- Returns:
- True if the display metrics are equal.
-
equalsPhysical
public boolean equalsPhysical(DisplayMetrics other)
Returns true if the physical aspects of the two display metrics are equal. This ignores the scaled density, which is a logical attribute based on the current desired font size.- Parameters:
other- The display metrics with which to compare.- Returns:
- True if the display metrics are equal.
-
hashCode
public int hashCode()
Description copied from class:ObjectReturns an integer hash code for this object. By contract, any two objects for whichObject.equals(java.lang.Object)returnstruemust return the same hash code value. This means that subclasses ofObjectusually override both methods or neither method.Note that hash values must not change over time unless information used in equals comparisons also changes.
See Writing a correct
hashCodemethod if you intend implementing your ownhashCodemethod.- Overrides:
hashCodein classObject- Returns:
- this object's hash code.
- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod.
-
-