Class ScreenDistance
- All Implemented Interfaces:
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 Summary
ConstructorDescriptionScreenDistance
(double distance, ScreenDistanceUnit unit) Explicit constructor taking a distance value and a unit. -
Method Summary
Modifier and TypeMethodDescriptiondouble
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()
getUnit()
Returns the screen distance unit.double
getValue()
Returns the screen distance value, as a double.static ScreenDistance
millimeters
(double millimeters) Utility factory method for constructing aScreenDistance
expressed in millimeters.static ScreenDistance
pixels
(double pixels) Utility factory method for constructing aScreenDistance
expressed in device independent pixels.
-
Constructor Details
-
ScreenDistance
Explicit constructor taking a distance value and a unit.- Parameters:
distance
- the distance, expressed in the chosen unit.unit
- eitherScreenDistanceUnit#Pixels
orScreenDistanceUnit#Millimeters
.
-
-
Method Details
-
finalize
protected void finalize() -
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
pixels
Utility factory method for constructing aScreenDistance
expressed in device independent pixels.- Parameters:
pixels
- the distance in device independent pixels.- Returns:
- a
ScreenDistance
instance taking the passed value and expressed in device independent pixels.
-
millimeters
Utility factory method for constructing aScreenDistance
expressed in millimeters.- Parameters:
millimeters
- the distance in millimeters.- Returns:
- a
ScreenDistance
instance taking the passed value and expressed in millimeters.
-
getValue
public double getValue()Returns the screen distance value, as a double.- Returns:
- the screen distance value, as a double.
-
getUnit
Returns the screen distance unit.- Returns:
- the screen distance unit.
-
asPixels
public double asPixels(double dpi, double displayScale) Converting value method which returns the distance value as a double, expressed in device independent pixels.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.- Parameters:
dpi
- the display density in dots per inchdisplayScale
- the display scale- Returns:
- the screen distance in device independent pixels, as a double.
-
asMillimeters
public double asMillimeters(double dpi, double displayScale) Converting value method which returns the distance value as a double, expressed in millimeters.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.- Parameters:
dpi
- the display density in dots per inchdisplayScale
- the display scale- Returns:
- the screen distance in millimeters, as a double.
-