Expand description
§Density-Independent Pixels (Dp)
This module provides the Dp
type for representing density-independent pixels,
a fundamental unit for UI scaling in the Tessera framework.
§Overview
Density-independent pixels (dp) are a virtual pixel unit that provides consistent visual sizing across different screen densities. Unlike physical pixels, dp units automatically scale based on the device’s screen density, ensuring that UI elements appear at the same physical size regardless of the display’s pixel density.
§Scale Factor
The conversion between dp and physical pixels is controlled by a global scale factor
stored in SCALE_FACTOR
. This factor is typically set based on the device’s DPI
(dots per inch) and user preferences.
§Usage
use tessera_ui::Dp;
// Create a dp value
let padding = Dp(16.0);
// Convert to pixels for rendering
let pixels = padding.to_pixels_f32();
// Create from pixel values
let dp_from_pixels = Dp::from_pixels_f32(48.0);
§Relationship with Px
The Dp
type works closely with the Px
type (physical pixels). You can
convert between them using the provided methods, with the conversion automatically
applying the current scale factor.
Structs§
- Dp
- Density-independent pixels (dp) for UI scaling.
Statics§
- SCALE_
FACTOR - Global scale factor for converting between density-independent pixels and physical pixels.