建構運算式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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"]]