build 表达式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
WFF 使用表达式语言来实现以下功能:
- 使用
Transform
或 Gyro
转换外观
- 通过
Condition
语句实现的条件行为
Template
元素中的字符串格式设置
表达式语言是一种脚本语言,其中包含常见的运算符和一系列可用的函数。
表达式可以使用数据源(使用方括号表示),以便您对外部输入(例如当前日期和时间、健康和健身指标,甚至天气)做出响应。
使用表达式时,Transform
或 Template
与 Condition
用法之间的主要区别在于,Transform
和 Template
要求表达式产生值(例如,封闭元素的新位置),而 Condition
要求表达式产生布尔值。
例如,Condition
可能会使用:
[DAY_OF_WEEK] == 6 || [DAY_OF_WEEK] == 7
此表达式会求值为布尔值,并使用 DAY_OF_WEEK
数据源确定是周末还是非周末。
还支持函数,例如,以下表达式基于 Wear OS 设备加速度计的 x-value
,用于在任一方向上将某个值旋转最多 5 度:
(5/90)*clamp([ACCELEROMETER_ANGLE_X],0,90) +
(-5/90)*clamp([ACCELEROMETER_ANGLE_X],-90,0)
clamp()
函数可将值限制在两个边界内。
重新评估表达式
表达式的重新评估频率取决于其中使用的各个数据源。例如,[DAY_OF_WEEK] == 6 || [DAY_OF_WEEK] ==
7
表达式仅在新的一天开始时重新求值。不过,使用 [SECOND]
数据源的表达式每秒重新评估一次。
根据表达式结果的变化,重新评估可能会导致场景重新计算和重新渲染。因此,请务必始终使用尽可能不经常重新评估的数据源。例如,如需确定当前是下午,请使用以下命令:
// Bad - re-evaluates every second
[SECONDS_IN_DAY] > 43200
// Good - limits re-evaluation frequency (1 = PM, 0 = AM)
[AMPM_STATE] == 1
表达式中的配置值
除了函数和数据源之外,您还可以使用配置值。例如,如果在 UserConfigurations 中定义了名为 showBackgroundInAfternoon
的 BooleanConfiguration
,则可以在表达式中使用它:
[CONFIGURATION.showBackgroundInAfternoon] == "TRUE" && [AMPM_STATE] == 1
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Build expressions\n\nWFF uses an expression language to enable:\n\n- Transforming the appearance using `Transform` or `Gyro`\n- Conditional behavior through `Condition` statements\n- String formatting in `Template` elements\n\nThe expression language is a scripting language which contains your\ntypical operators and a range of functions that can be used.\n\nExpressions can use [data sources](/training/wearables/wff/common/attributes/source-type)---represented using square brackets---to\nlet you react to external inputs such as the current date and time, health\nand fitness metrics, or even the weather.\n\nWhen using expressions, the primary difference between `Transform` or `Template` and\n`Condition` usage, is that `Transform` and `Template` require the expression to result\nin a *value* (for example, the new position of the enclosing element) whereas\n`Condition` requires the expression to result in a *boolean*.\n\nFor example, a `Condition` might use: \n\n [DAY_OF_WEEK] == 6 || [DAY_OF_WEEK] == 7\n\nThis evaluates to a boolean and determines whether it is a weekend or not,\nusing the `DAY_OF_WEEK` data source.\n\n[Functions](/training/wearables/wff/common/attributes/arithmetic-expression#functions) are also supported---for example, an expression for rotating a\nvalue up to 5 degrees in either direction---based on the `x-value` of the Wear OS\ndevice's accelerometer: \n\n (5/90)*clamp([ACCELEROMETER_ANGLE_X],0,90) +\n (-5/90)*clamp([ACCELEROMETER_ANGLE_X],-90,0)\n\nThe `clamp()` function constrains a value within two bounds.\n\n### Expression re-evaluation\n\nThe frequency with which expressions are re-evaluated depends on the data\nsources used in them. For example, the \\[`DAY_OF_WEEK] == 6 || [DAY_OF_WEEK] ==\n7` expression only re-evaluates when a new day starts. However, an expression\nthat uses the `[SECOND]` data source re-evaluates every second.\n\nRe-evaluation may result in scene recalculations and rerendering, based on the\nchange in the result of the expression. Therefore it is important to always use\ndata sources that re-evaluate as infrequently as possible. For example, to\ndetermine whether it is afternoon: \n\n // Bad - re-evaluates every second\n [SECONDS_IN_DAY] \u003e 43200\n\n // Good - limits re-evaluation frequency (1 = PM, 0 = AM)\n [AMPM_STATE] == 1\n\n### Configuration values in expressions\n\nIn addition to functions and data sources, configuration values can be used. For\nexample, if in the [UserConfigurations](/training/wearables/wff/user-configuration/user-configurations) a `BooleanConfiguration` named\n`showBackgroundInAfternoon` has been defined, this can be used in an expression: \n\n [CONFIGURATION.showBackgroundInAfternoon] == \"TRUE\" && [AMPM_STATE] == 1"]]