通用类型

查看 View 属性、数据类型和代码示例。

ViewContent

可渲染的子内容:View、基础值或嵌套数组;null/undefined 不产生内容。

type ViewContent =
  View | string | number | boolean | null | undefined | ViewContent[];

示例

const example: ViewContent = ['Speed', 78, null];

TextContent

文字内容统一调用 toString();null/undefined 留空。图标请通过其他 View 组合。

type TextContent =
  | string
  | number
  | bigint
  | boolean
  | symbol
  | { toString(): string }
  | null
  | undefined;

示例

const example: TextContent = {
  toString() {
    return '78 km/h';
  },
};

HDRColor

可用于颜色和渐变的 HDR 颜色数据。

type HDRColor = { readonly $type: 'CapDrive.HDR'; readonly color: string };

示例

const example: HDRColor = HDR('white');

RGBColor

r/g/b 及可选透明度 a 的范围为 0–1;space 选择 srgb 或 displayP3 色彩空间。

type RGBColor = {
  space?: 'srgb' | 'displayP3';
  r: number;
  g: number;
  b: number;
  a?: number;
};

示例

const example: RGBColor = {
  space: 'displayP3',
  r: 0.2,
  g: 0.7,
  b: 0.6,
  a: 0.8,
};

Color

颜色名称、十六进制字符串、RGBColor 对象或 HDR() 颜色。

type Color = string | RGBColor | HDRColor;

示例

const example: Color = '#30B3A2';

ViewPoint

以屏幕点为单位的局部 x/y 坐标,不是经纬度。

type ViewPoint = { x: number; y: number };

示例

const example: ViewPoint = { x: 20, y: 40 };

ChartDataPoint

折线图数据点:value.x/y 决定位置,label.x/y 决定文案;y 为 null 时断开曲线。

type ChartDataPoint = {
  value: { x: number; y: number | null };
  label: { x: string; y: string };
};

示例

const example: ChartDataPoint = {
  value: { x: 12, y: null },
  label: { x: '12s', y: '' },
};

ChartRange

数值范围 [min, max],不是数据项索引,也不是标签。

type ChartRange = readonly [min: number, max: number];

示例

const example: ChartRange = [-20, 100];

MapCoordinate

地理坐标 latitude 纬度、longitude 经度,单位为度。

type MapCoordinate = { latitude: number; longitude: number };

示例

const example: MapCoordinate = { latitude: 31.2304, longitude: 121.4737 };

MapPathStyle

原生轨迹样式:Color 颜色、线宽、端点、连接、虚线、外描边及可选起终点标记。不支持渐变。

type MapPathStyle = {
  stroke?: Color;
  lineWidth?: number;
  lineCap?: 'round' | 'butt' | 'square';
  lineJoin?: 'round' | 'miter' | 'bevel';
  dash?: readonly number[];
  dashPhase?: number;
  outlineStroke?: Color;
  outlineWidth?: number;
  showEndpoints?: boolean;
};

示例

const example: MapPathStyle = {
  stroke: '#30B3A2',
  lineWidth: 4,
  outlineStroke: 'white',
  outlineWidth: 1,
  showEndpoints: true,
};

ChartAxisStyle

轴线的描边颜色或渐变、宽度、端点和虚线样式。clear 隐藏轴线,标签由各自开关控制。

type ChartAxisStyle = {
  stroke?: Paint;
  lineWidth?: number;
  lineCap?: 'round' | 'butt' | 'square';
  dash?: readonly number[];
  dashPhase?: number;
};

示例

const example: ChartAxisStyle = {
  stroke: '#888888',
  lineWidth: 1,
  dash: [4, 3],
};

BarChartDataPoint

一根柱子的数值 value 和可选分类标签 label;数组顺序决定排列顺序。

type BarChartDataPoint = { value: number; label?: string };

示例

const example: BarChartDataPoint = { value: -25, label: 'B' };

PathCommand

路径指令 move、line、quad、cubic、arc、close;坐标和半径用点,圆弧角度用度。

type PathCommand =
  | { op: 'move'; x: number; y: number }
  | { op: 'line'; x: number; y: number }
  | { op: 'quad'; x: number; y: number; cx: number; cy: number }
  | {
      op: 'cubic';
      x: number;
      y: number;
      c1x: number;
      c1y: number;
      c2x: number;
      c2y: number;
    }
  | {
      op: 'arc';
      x: number;
      y: number;
      radius: number;
      startAngle: number;
      sweepAngle: number;
    }
  | { op: 'close' };

示例

const example: PathCommand = {
  op: 'cubic',
  x: 100,
  y: 60,
  c1x: 20,
  c1y: 0,
  c2x: 80,
  c2y: 100,
};

GradientStop

一个颜色,或带有 location 的颜色对象;location 是渐变中的 0–1 位置。

type GradientStop = Color | { color: Color; location: number };

示例

const example: GradientStop = { color: '#30B3A2', location: 0.7 };

GradientColors

按顺序排列的渐变颜色或色标;只填颜色时等距分布。

type GradientColors = ReadonlyArray<GradientStop>;

示例

const example: GradientColors = [
  { color: 'red', location: 0 },
  { color: 'blue', location: 1 },
];

LinearGradientOptions

线性渐变的 angle 用度,center 为归一化坐标;(0.5, 0.5) 表示中心。

type LinearGradientOptions = { angle?: number; center?: ViewPoint };

示例

const example: LinearGradientOptions = {
  angle: 90,
  center: { x: 0.5, y: 0.5 },
};

RadialGradientOptions

径向渐变的 startRadius/endRadius 用点,center 为归一化坐标。

type RadialGradientOptions = {
  startRadius?: number;
  endRadius?: number;
  center?: ViewPoint;
};

示例

const example: RadialGradientOptions = {
  startRadius: 0,
  endRadius: 60,
  center: { x: 0.5, y: 0.5 },
};

AngularGradientOptions

角向渐变的 angle 用度,center 为归一化坐标。

type AngularGradientOptions = { angle?: number; center?: ViewPoint };

示例

const example: AngularGradientOptions = {
  angle: -90,
  center: { x: 0.5, y: 0.5 },
};

Gradient

由 LinearGradient、RadialGradient 或 AngularGradient 创建的渐变;可直接调用,也可使用 new。

type Gradient = {
  readonly type: 'linear' | 'radial' | 'angular';
  readonly angle?: number;
  readonly startRadius?: number;
  readonly endRadius?: number;
  readonly center?: ViewPoint;
  readonly colors: GradientColors;
};

示例

const example: Gradient = LinearGradient(['red', 'blue'], { angle: 90 });

Paint

绘制用的填色,可以是 Color 或 Gradient。

type Paint = Color | Gradient;

示例

const example: Paint = RadialGradient(['white', '#30B3A2'], {
  endRadius: 60,
});

DrawingEffect

特效的颜色、不透明度、模糊半径和 x/y 偏移;距离单位为点。

type DrawingEffect = {
  color?: Color;
  opacity?: number;
  radius?: number;
  x?: number;
  y?: number;
};

示例

const example: DrawingEffect = {
  color: 'cyan',
  opacity: 0.6,
  radius: 8,
  x: 0,
  y: 2,
};

ViewTransform

对整个 View 做视觉变换,不改变布局。平移用点,旋转和倾斜用角度,缩放 1 保持原大小,锚点用 0–1。

type ViewTransform = {
  translateX?: number;
  translateY?: number;
  scaleX?: number;
  scaleY?: number;
  rotation?: number;
  skewX?: number;
  skewY?: number;
  anchorX?: number;
  anchorY?: number;
};

示例

const example: ViewTransform = {
  rotation: 30,
  scaleX: 1.2,
  scaleY: 1.2,
  anchorX: 0.5,
  anchorY: 0.5,
};

RepeatAxisValue

填写一个数时作用于启用的轴;也可用 x/y 对象分别配置。

type RepeatAxisValue = number | { x?: number; y?: number };

示例

const example: RepeatAxisValue = { x: 120, y: 80 };

AnimationProperty

可动画的属性名或嵌套路径;每种 View 只支持其列出的子集。

type AnimationProperty =
  | 'width'
  | 'height'
  | 'padding'
  | 'x'
  | 'y'
  | 'opacity'
  | 'cornerRadius'
  | 'blur'
  | 'shadowOpacity'
  | 'shadowRadius'
  | 'shadowX'
  | 'shadowY'
  | 'fontSize'
  | 'letterSpacing'
  | 'minimumScaleFactor'
  | 'spacing'
  | 'radius'
  | 'radiusX'
  | 'radiusY'
  | 'startAngle'
  | 'sweepAngle'
  | 'lineWidth'
  | 'dashPhase'
  | 'trimStart'
  | 'trimEnd'
  | 'barThickness'
  | 'value'
  | 'offset'
  | 'anchor'
  | 'period'
  | 'minLength'
  | 'color'
  | 'stroke'
  | 'background'
  | 'shadowColor'
  | 'transform.translateX'
  | 'transform.translateY'
  | 'transform.scaleX'
  | 'transform.scaleY'
  | 'transform.rotation'
  | 'transform.skewX'
  | 'transform.skewY'
  | 'transform.anchorX'
  | 'transform.anchorY'
  | 'period.x'
  | 'period.y'
  | 'value.x'
  | 'value.y'
  | 'anchor.x'
  | 'anchor.y'
  | 'offset.x'
  | 'offset.y'
  | 'start.x'
  | 'start.y'
  | 'end.x'
  | 'end.y'
  | 'rangeX.0'
  | 'rangeX.1'
  | 'rangeY.0'
  | 'rangeY.1'
  | 'xAxis.lineWidth'
  | 'xAxis.dashPhase'
  | 'yAxis.lineWidth'
  | 'yAxis.dashPhase'
  | 'xAxis.stroke'
  | 'yAxis.stroke'
  | 'regionOffset.x'
  | 'regionOffset.y'
  | 'pathStyle.lineWidth'
  | 'pathStyle.outlineWidth'
  | 'pathStyle.dashPhase'
  | 'pathStyle.stroke'
  | 'pathStyle.outlineStroke';

示例

const example: AnimationProperty = 'transform.rotation';

Animation

支持的属性变化时执行动画。duration/delay 单位为秒;from/to 指定端点;repeat 和 autoreverse 控制重复,wrap 指定周期。

type Animation = {
  property: AnimationProperty;
  duration?: number;
  delay?: number;
  curve?: string;
  from?: number | Color;
  to?: number | Color;
  repeat?: number | 'forever';
  autoreverse?: boolean;
  wrap?: number;
};

示例

const example: Animation = {
  property: 'opacity',
  duration: 0.25,
  curve: 'easeOut',
};

CircleAnimation

圆弧类图形或布局的动画。sweepAngle 使用数值端点,不支持 wrap;startAngle 可使用 wrap: 360。

type CircleAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'dashPhase'
        | 'stroke'
        | 'lineWidth'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

示例

const example: CircleAnimation = {
  property: 'sweepAngle',
  duration: 0.3,
  from: 0,
  to: 270,
};

EllipseAnimation

圆弧类图形或布局的动画。sweepAngle 使用数值端点,不支持 wrap;startAngle 可使用 wrap: 360。

type EllipseAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'dashPhase'
        | 'stroke'
        | 'lineWidth'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

示例

const example: EllipseAnimation = {
  property: 'startAngle',
  duration: 0.3,
  wrap: 360,
};

ArcAnimation

圆弧类图形或布局的动画。sweepAngle 使用数值端点,不支持 wrap;startAngle 可使用 wrap: 360。

type ArcAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'dashPhase'
        | 'stroke'
        | 'lineWidth'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

示例

const example: ArcAnimation = {
  property: 'sweepAngle',
  duration: 0.3,
  from: 0,
  to: -180,
};

MapAnimation

地图动画作用于支持的 View 和路线样式属性;原生相机变化另由 cameraAnimation 控制。

type MapAnimation =
  | (Omit<Animation, 'property' | 'from' | 'to'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'cornerRadius'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY'
        | 'regionOffset.x'
        | 'regionOffset.y'
        | 'pathStyle.lineWidth'
        | 'pathStyle.outlineWidth'
        | 'pathStyle.dashPhase';
      from?: number;
      to?: number;
    })
  | (Omit<Animation, 'property' | 'from' | 'to' | 'wrap'> & {
      property: 'pathStyle.stroke' | 'pathStyle.outlineStroke';
      from?: Color;
      to?: Color;
      wrap?: never;
    });

示例

const example: MapAnimation = {
  property: 'pathStyle.stroke',
  duration: 0.25,
  from: 'red',
  to: 'blue',
};

LineChartAnimation

图表动画还支持范围和轴线样式;范围变化不使用 wrap,颜色路径接受颜色值。

type LineChartAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'stroke'
        | 'lineWidth'
        | 'dashPhase'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'rangeX.0' | 'rangeX.1' | 'rangeY.0' | 'rangeY.1';
      from?: number;
      to?: number;
      wrap?: never;
    })
  | (Omit<Animation, 'property' | 'from' | 'to'> & {
      property:
        | 'xAxis.lineWidth'
        | 'xAxis.dashPhase'
        | 'yAxis.lineWidth'
        | 'yAxis.dashPhase';
      from?: number;
      to?: number;
    })
  | (Omit<Animation, 'property' | 'from' | 'to' | 'wrap'> & {
      property: 'xAxis.stroke' | 'yAxis.stroke';
      from?: Color;
      to?: Color;
      wrap?: never;
    });

示例

const example: LineChartAnimation = { property: 'rangeX.1', duration: 0.25 };

BarChartAnimation

图表动画还支持范围和轴线样式;范围变化不使用 wrap,颜色路径接受颜色值。

type BarChartAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'cornerRadius'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'barThickness'
        | 'stroke'
        | 'lineWidth'
        | 'dashPhase'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'rangeY.0' | 'rangeY.1';
      from?: number;
      to?: number;
      wrap?: never;
    })
  | (Omit<Animation, 'property' | 'from' | 'to'> & {
      property:
        | 'xAxis.lineWidth'
        | 'xAxis.dashPhase'
        | 'yAxis.lineWidth'
        | 'yAxis.dashPhase';
      from?: number;
      to?: number;
    })
  | (Omit<Animation, 'property' | 'from' | 'to' | 'wrap'> & {
      property: 'xAxis.stroke' | 'yAxis.stroke';
      from?: Color;
      to?: Color;
      wrap?: never;
    });

示例

const example: BarChartAnimation = { property: 'rangeY.1', duration: 0.25 };

RadialLayoutAnimation

圆弧类图形或布局的动画。sweepAngle 使用数值端点,不支持 wrap;startAngle 可使用 wrap: 360。

type RadialLayoutAnimation =
  | (Omit<Animation, 'property'> & {
      property:
        | 'width'
        | 'height'
        | 'padding'
        | 'x'
        | 'y'
        | 'opacity'
        | 'background'
        | 'cornerRadius'
        | 'blur'
        | 'shadowColor'
        | 'shadowOpacity'
        | 'shadowRadius'
        | 'shadowX'
        | 'shadowY'
        | 'radius'
        | 'radiusX'
        | 'radiusY'
        | 'startAngle'
        | 'transform.translateX'
        | 'transform.translateY'
        | 'transform.scaleX'
        | 'transform.scaleY'
        | 'transform.rotation'
        | 'transform.skewX'
        | 'transform.skewY'
        | 'transform.anchorX'
        | 'transform.anchorY';
    })
  | (Omit<Animation, 'property' | 'wrap' | 'from' | 'to'> & {
      property: 'sweepAngle';
      from?: number;
      to?: number;
      wrap?: never;
    });

示例

const example: RadialLayoutAnimation = {
  property: 'startAngle',
  duration: 0.25,
  wrap: 360,
};

SpacerAnimation

Spacer 只支持 width、height、minLength 的布局动画,不包含视觉特效。

type SpacerAnimation = {
  property: 'width' | 'height' | 'minLength';
  duration?: number;
  delay?: number;
  curve?: string;
  from?: number;
  to?: number;
  repeat?: number | 'forever';
  autoreverse?: boolean;
  wrap?: number;
};

示例

const example: SpacerAnimation = {
  property: 'minLength',
  duration: 0.25,
  from: 8,
  to: 24,
};

ContentAnimation

内容变化时的过渡效果;选择该 View 支持的 type,duration/delay 单位为秒。

type ContentAnimation = {
  type: 'fade' | 'slide' | 'scale' | 'numericText' | 'interpolate';
  duration?: number;
  delay?: number;
  curve?: string;
  direction?: string;
  distance?: number;
  scale?: number;
};

示例

const example: ContentAnimation = { type: 'numericText', duration: 0.18 };

ViewSize

测量得到的 width 宽和 height 高,单位为屏幕点。

type ViewSize = { width: number; height: number };

示例

const example: ViewSize = { width: 240, height: 140 };

ViewInsets

上、起始侧、下、末尾侧的内边距,单位为点。

type ViewInsets = {
  top: number;
  leading: number;
  bottom: number;
  trailing: number;
};

示例

const example: ViewInsets = {
  top: 24,
  leading: 12,
  bottom: 12,
  trailing: 12,
};

CardContentMetrics

传给 renderer 的卡片总尺寸 size、正文 contentSize、标题 headerSize 和安全边距 safeInsets,用于避开标题安排正文。

type CardContentMetrics = {
  size: ViewSize;
  contentSize: ViewSize;
  headerSize: ViewSize;
  safeInsets: ViewInsets;
};

示例

const example: CardContentMetrics = {
  size: { width: 240, height: 160 },
  contentSize: { width: 216, height: 124 },
  headerSize: { width: 216, height: 24 },
  safeInsets: { top: 24, leading: 12, bottom: 12, trailing: 12 },
};