应用遮罩、混合效果和色调

GroupPart* 元素还提供 tintColor, renderModeblendMode 属性,它们是调整表盘各部分外观的强大方式。

将剪裁遮罩与渲染模式搭配使用

WFF 版本 1 中引入了 renderMode,以实现剪裁遮罩。

renderMode 可应用于场景层次结构中的 GroupPart* 元素。为了更好地了解其运作方式,我们来看看场景图的渲染方式:

<Group>
  <Group>
    <PartDraw />
    <PartText />
  </Group>
  <PartImage />
</Group>
INSERT ALT TEXT HERE

表盘渲染程序在遍历场景树时按顺序绘制元素。

renderMode 应用于节点时,该效果仅应用于该节点的父 Group 中。图中其他位置的其他节点不受影响。

如果未指定 renderMode,则默认为 SOURCE,这意味着元素会直接绘制到屏幕上。不过,如果父节点中存在一个或多个指定了 MASK(或 ALL)的元素,则会使用其他方法:

  1. 创建一个屏幕外画布
  2. 系统会绘制设置了 SOURCE(默认值)的所有子元素
    1. 掩码 (MASK, ALL) 的所有子元素都会应用于画布,以剪裁生成的图片。

请注意,这意味着系统不会考虑父节点中元素的顺序。

在以下示例中,Scene 父级包含三个子级:

  • 第一个和第三个元素是 MASK 元素,因此它们会组合在一起形成遮罩层
  • 第二个元素是唯一的非遮罩层
<WatchFace width="450" height="450">
  <Scene>
    <PartDraw x="175" y="50" width="100" height="100" renderMode="MASK">
      <Ellipse x="0" y="0" width="100" height="100">
        <Fill color="#FFFFFF"></Fill>
      </Ellipse>
    </PartDraw>

    <PartDraw x="0" y="0" width="450" height="450">
      <Rectangle x="0" y="0" width="450" height="450">
        <Fill color="#ff0000">
          <LinearGradient startX="0" startY="0" endX="450" endY="450"
            colors="..." positions="..." />
        </Fill>
      </Rectangle>
    </PartDraw>

    <PartText x="75" y="250" width="300" height="80" renderMode="MASK">
      <Text align="CENTER">
        <Font family="pacifico" size="72" weight="BOLD" color="#FFFFFF">Hello!</Font>
      </Text>
    </PartText>
  </Scene>
</WatchFace>

除了 SOURCEMASK 之外,renderMode 的第三个选项是 ALLALL 指定应将该元素同时视为 SOURCEMASK,这在某些情况下会很有用。

使用混合模式

注意:此功能适用于表盘格式 3 及更高版本。

从版本 3 开始,表盘格式在组合 Part* 元素时提供了应用混合模式的功能。

renderMode 不同(它引入了分别用于构建源和遮罩的特殊机制),blendMode 提供对所有 Android 图形混合模式效果的通用访问权限,并按照元素在场景图中显示的顺序应用效果。

在正常操作期间,当场景中放置两个 Part* 元素时,最上面的元素会遮盖或部分遮盖下面的元素,因为默认的 blendModeSRC_OVER

<PartDraw x="25" y="15" width="150" height="150">
  <Rectangle x="0" y="0" width="150" height="150">
    <Fill color="#ffd3ba" />
  </Rectangle>
</PartDraw>
<PartText x="35" y="60" width="300" height="120">
  <Text align="START">
    <Font family="pacifico" size="96" weight="BOLD" color="#fb5607">Hello!</Font>
  </Text>
</PartText>

为了演示如何使用混合模式,选择 SRC_ATOP 会丢弃未覆盖目标像素的源像素。也就是说,PartText 是来源,PartDraw 是目标。

因此,文本只会绘制在矩形上,而不会绘制在表盘的其他位置:

<PartDraw x="25" y="15" width="150" height="150">
  <Rectangle x="0" y="0" width="150" height="150">
    <Fill color="#ffd3ba" />
  </Rectangle>
</PartDraw>
<PartText x="35" y="60" width="300" height="120" blendMode="SRC_ATOP">
  <Text align="START">
    <Font family="pacifico" size="96" weight="BOLD" color="#fb5607">Hello!</Font>
  </Text>
</PartText>

您还可以应用更复杂的效果,例如使用 COLOR_DODGE 而非 SRC_ATOP,这会使目标更亮以反映来源

使用 HUECOLOR_BURN 混合模式组合多个 Part* 元素的示例:

<Group name="container" x="75" y="100" width="300" height="300">
  <PartDraw x="25" y="15" width="150" height="150">
    <Rectangle x="0" y="0" width="150" height="150">
      <Fill color="#ffd3ba" />
    </Rectangle>
  </PartDraw>
  <PartDraw x="100" y="15" width="150" height="150" blendMode="HUE">
    <Ellipse x="0" y="0" width="150" height="150">
      <Fill color="#219ebc" />
    </Ellipse>
  </PartDraw>
  <PartText x="35" y="60" width="300" height="120" blendMode="COLOR_BURN">
    <Text align="START">
      <Font family="pacifico" size="96" weight="BOLD" color="#fb5607">Hello!</Font>
    </Text>
  </PartText>
</Group>
INSERT ALT TEXT HERE

没有任何 blendMode 效果

INSERT ALT TEXT HERE

blendMode 效果

注意事项

以下部分介绍了使用剪裁和混合效果时需要注意的一些事项。

混合模式会在渲染模式之前应用

如果某个节点同时包含使用 blendModePart 元素和使用 renderMode(已设置为 MASKALL)的 Part 元素,则系统会采用以下方法。

  1. 源代码会进行合成,包括应用 blendMode 模式
  2. 然后,系统会从指定 rendermode="MASK 的元素应用遮罩

例如,考虑之前使用的示例,并添加一个矩形遮罩,请注意,被遮罩元素的顺序无关紧要:

<Group name="container" x="75" y="100" width="300" height="300">
  <PartDraw x="25" y="15" width="150" height="150">
    <Rectangle x="0" y="0" width="150" height="150">
      <Fill color="#ffd3ba" />
    </Rectangle>
  </PartDraw>
  <PartDraw x="100" y="15" width="150" height="150" blendMode="HUE">
    <Ellipse x="0" y="0" width="150" height="150">
      <Fill color="#219ebc" />
    </Ellipse>
  </PartDraw>
  <PartDraw x="100" y="15" width="150" height="150" renderMode="MASK">
    <Rectangle x="0" y="0" width="150" height="150">
      <Fill color="#ffffff" />
    </Rectangle>
  </PartDraw>
  <PartText x="35" y="60" width="300" height="120" blendMode="COLOR_BURN">
    <Text align="START">
      <Font family="pacifico" size="96" weight="BOLD" color="#fb5607">Hello!</Font>
    </Text>
  </PartText>
</Group>

性能

使用 renderModeblendMode 都需要额外的计算和绘制步骤。具体取决于表盘所运行的设备,其中一些操作可能会在受支持的硬件中高效执行,但在旧款设备上可能无法执行。

因此,请谨慎使用 renderModeblendMode,并注意它们对表盘整体性能的影响。

使用色调

tintColor 可应用于整个 Part* 元素、Group 或单个指针(例如 HourHandMinuteHand),例如:

<WatchFace width="450" height="450">
  <Scene>
    <Group x="75" y="100" width="350" height="350" name="group1" tintColor="#ffd3ba">
      <PartDraw x="25" y="0" width="100" height="100">
        <Rectangle x="0" y="0" width="100" height="100">
          <Fill color="#ffffff" />
        </Rectangle>
      </PartDraw>
      <PartDraw x="150" y="0" width="100" height="100">
        <Ellipse x="25" y="0" width="100" height="100">
          <Fill color="#ffffff" />
        </Ellipse>
      </PartDraw>
      <PartText x="0" y="150" width="300" height="80">
        <Text align="CENTER">
          <Font family="pacifico" size="72" weight="BOLD" color="#ffffff">Hello!</Font>
        </Text>
      </PartText>
    </Group>
  </Scene>
</WatchFace>

这对于设置表盘的整个部分的样式非常有用,包括应用用户设置中的样式,例如:tintcolor="[CONFIGURATION.myThemeColor.0]"