Group 和 Part* 元素还提供 tintColor, renderMode 和 blendMode 属性,这些属性是调整表盘部分外观的强大方法。
将剪切蒙版与渲染模式搭配使用
renderMode 是在 WFF 的第 1 版中引入的,用于实现剪切蒙版。
renderMode 可应用于场景层次结构中的 Group 和 Part* 元素。表盘渲染程序在遍历场景树时会按顺序绘制元素。
将 renderMode 应用于某个节点时,该效果仅在该节点的父级 Group 中应用。图表中其他位置的其他节点不受影响。
如果未指定 renderMode,则默认值为 SOURCE,这意味着元素会直接绘制到屏幕上。但是,如果父节点中存在一个或多个指定了 MASK(或 ALL)的元素,则会使用不同的方法:
- 创建了离屏画布
- 所有设置了
SOURCE(默认值)的子元素都会绘制- 属于遮罩 (
MASK, ALL) 的所有子元素都会应用于画布,以剪裁生成的图片。
- 属于遮罩 (
请注意,这意味着系统不会考虑父节点中元素的顺序。
在以下示例中,Scene 父级包含三个子级:
- 第一个和第三个元素是
MASK元素,因此它们会组合在一起形成遮罩层 - 第二个元素是唯一的非遮罩层
<PartDraw x="175" y="50" width="100" height="100" renderMode="MASK"> <Ellipse x="0" y="0" width="100" height="100"> <Fill color="#FFFFFF" /> </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="#FFFF00 #00FFFF #FF00FF" positions="0.25 0.5 0.75" /> </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>
除了 SOURCE 和 MASK 之外,renderMode 的第三个选项是 ALL。
ALL 指定应将元素同时视为 SOURCE 和 MASK,这在某些场景中可能很有用。
使用混合模式
注意:此功能适用于表盘格式版本 3 及更高版本。
从版本 3 开始,表盘格式支持在组合 Part* 元素时应用混合模式。
renderMode 引入了一种特殊机制,分别用于构建来源和遮罩,而 blendMode 则不同,它提供对所有 Android Graphics 混合模式效果的常规访问权限,并按照元素在场景图中的显示顺序应用效果。
在正常操作中,当两个 Part* 元素放置在场景中时,最顶部的元素会遮挡或部分遮挡较低的元素,因为默认 blendMode 为 SRC_OVER。
<PartDraw x="125" y="115" width="150" height="150"> <Rectangle x="0" y="0" width="150" height="150"> <Fill color="#ffd3ba" /> </Rectangle> </PartDraw> <PartText x="135" y="160" 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="125" y="115" width="150" height="150"> <Rectangle x="0" y="0" width="150" height="150"> <Fill color="#ffd3ba" /> </Rectangle> </PartDraw> <PartText x="135" y="160" 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,这会使目标更亮以反映来源。
使用 HUE 和 COLOR_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>
注意事项
以下部分介绍了使用剪辑和混合效果时需要注意的一些事项。
混合模式在渲染模式之前应用
如果节点同时包含使用 blendMode 的 Part 元素和使用 renderMode(设置为 MASK 或 ALL)的 Part 元素,则会采用以下方法。
- 来源已合成,包括应用
blendMode模式 - 然后,系统会从指定
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>
性能
使用 renderMode 和 blendMode 都需要额外的计算和绘制步骤。根据运行表盘的设备,其中一些操作可以在受支持的硬件中高效执行,但在旧设备上可能无法执行。
因此,请谨慎使用 renderMode 和 blendMode,并注意使用它们可能会对表盘的整体性能产生的影响。
使用色调
tintColor 可应用于整个 Part* 元素 Group,或应用于各个指针(例如 HourHand 和 MinuteHand),例如:
<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>
这对于为表盘的整个部分设置样式非常有用,包括应用用户设置中的样式,例如:tintcolor="[CONFIGURATION.myThemeColor.0]"。