public final class ScreenDistance extends Object implements AutoCloseable
It consists of a unit (expressed as a ScreenDistanceUnit) and the distance value.
This class contains utility methods for converting distance values from millimeters to pixels or vice-versa, by taking DPI and display scale into account (see ScreenDistance#asPixels
and ScreenDistance#asMillimeters
).
Related article: Support high-resolution (HiDPI) displays.
Constructor and Description |
---|
ScreenDistance(double distance,
ScreenDistanceUnit unit)
Explicit constructor taking a distance value and a unit.
|
Modifier and Type | Method and Description |
---|---|
double |
asMillimeters(double dpi,
double displayScale)
Converting value method which returns the distance value as a double, expressed in millimeters.
|
double |
asPixels(double dpi,
double displayScale)
Converting value method which returns the distance value as a double, expressed in device independent pixels.
|
void |
close() |
protected void |
finalize() |
ScreenDistanceUnit |
getUnit() |
double |
getValue() |
static ScreenDistance |
millimeters(double millimeters)
Utility factory method for constructing a
ScreenDistance expressed in millimeters. |
static ScreenDistance |
pixels(double pixels)
Utility factory method for constructing a
ScreenDistance expressed in device independent pixels. |
public ScreenDistance(double distance, @NotNull ScreenDistanceUnit unit)
distance
- the distance, expressed in the chosen unit.unit
- either ScreenDistanceUnit#Pixels
or ScreenDistanceUnit#Millimeters
.public void close()
close
in interface AutoCloseable
@NotNull public static ScreenDistance pixels(double pixels)
ScreenDistance
expressed in device independent pixels.pixels
- the distance in device independent pixels.ScreenDistance
instance taking the passed value and expressed in device independent pixels.@NotNull public static ScreenDistance millimeters(double millimeters)
ScreenDistance
expressed in millimeters.millimeters
- the distance in millimeters.ScreenDistance
instance taking the passed value and expressed in millimeters.public double getValue()
@NotNull public ScreenDistanceUnit getUnit()
public double asPixels(double dpi, double displayScale)
In order to make any possible conversion from millimeters to pixels, the display DPI and display scale must be provided. If the stored unit is ScreenDistanceUnit#Pixels
, the currently stored value is returned.
dpi
- the display density in dots per inchdisplayScale
- the display scalepublic double asMillimeters(double dpi, double displayScale)
In order to make any possible conversion from device independent pixels to millimeters, the display DPI and display scale must be provided. If the stored unit is ScreenDistanceUnit#Millimeters
, the currently stored value is returned.
dpi
- the display density in dots per inchdisplayScale
- the display scale