Index | Files

package unit

import ""

Package unit implements device independent units and values.

A Value is a value with a Unit attached.

Device independent pixel, or dp, is the unit for sizes independent of the underlying display device.

Scaled pixels, or sp, is the unit for text sizes. An sp is like dp with text scaling applied.

Finally, pixels, or px, is the unit for display dependent pixels. Their size vary between platforms and displays.

To maintain a constant visual size across platforms and displays, always use dps or sps to define user interfaces. Only use pixels for derived values.


Package Files


type Metric Uses

type Metric struct {
    // PxPerDp is the device-dependent pixels per dp.
    PxPerDp float32
    // PxPerSp is the device-dependent pixels per sp.
    PxPerSp float32

Metric converts Values to device-dependent pixels, px. The zero value represents a 1-to-1 scale from dp, sp to pixels.

func (Metric) Px Uses

func (c Metric) Px(v Value) int

type Unit Uses

type Unit uint8

Unit represents a unit for a Value.

const (
    // UnitPx represent device pixels in the resolution of
    // the underlying display.
    UnitPx Unit = iota
    // UnitDp represents device independent pixels. 1 dp will
    // have the same apparent size across platforms and
    // display resolutions.
    // UnitSp is like UnitDp but for font sizes.

func (Unit) String Uses

func (u Unit) String() string

type Value Uses

type Value struct {
    V   float32
    U   Unit

Value is a value with a unit.

func Add Uses

func Add(c Metric, values ...Value) Value

Add a list of Values.

func Dp Uses

func Dp(v float32) Value

Dp returns the Value for v device independent pixels.

func Max Uses

func Max(c Metric, values ...Value) Value

Max returns the maximum of a list of Values.

func Px Uses

func Px(v float32) Value

Px returns the Value for v device pixels.

func Sp Uses

func Sp(v float32) Value

Sp returns the Value for v scaled dps.

func (Value) Scale Uses

func (v Value) Scale(s float32) Value

Scale returns the value scaled by s.

func (Value) String Uses

func (v Value) String() string

Package unit imports 2 packages (graph) and is imported by 100 packages. Updated 2020-11-27. Refresh now. Tools for package owners.