Wear OS の動的式
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Wear OS では、タイルとウォッチフェイスの追加機能に表示される情報の動的な更新をサポートしています。
動的式を使用すると、アプリのサーフェス(タイルやウォッチフェイスの追加機能など)に表示されるデータを特定のデータソースにバインドできます。そのようなデータソースには、プラットフォームが読み取れる心拍数データなどがあります。このバインディングを設定すると、システムはタイルとウォッチフェイスの追加機能のデータを自動的に更新します。
動的データ バインディングを作成する
動的データ バインディングを作成するには、動的データ型を使用する変数を指定します。この変数を使用するデータ ストリームに関連付けます。
たとえば、次のコード スニペットに示すように、システム クロックと健康情報に関連する値を取得できます。
Kotlin
val systemTime = DynamicInstant.platformTimeWithSecondsPrecision()
val steps: DynamicInt32 = PlatformHealthSources.dailySteps()
Java
DynamicInstant systemTime = DynamicInstant.platformTimeWithSecondsPrecision();
DynamicInt32 steps = PlatformHealthSources.dailySteps();
次のスニペットに示すように、定数式から動的な値を作成し、動的な値に対して算術演算を実行することもできます。
Kotlin
val dynamicValue = DynamicInt32.constant(1).plus(2)
Java
DynamicInt32 dynamicValue = DynamicInt32.constant(1).plus(2)
有効な動的データ型のリスト
Wear OS は次の動的データ型をサポートしています。
また、次のような組み込みの機能を使用して、データ型を変換することもできます。
DynamicInt32
は format()
を使用した DynamicString
への変換をサポートしています。
DynamicDuration
では時間の秒数などの特定の部分を DynamicInt32
オブジェクトとして抽出できます。
各画面で限られた数の動的式を使用する
特定の画面で同時に処理できる動的式の数には上限があります。上限を上回る動的式はシステムで静的な値に変換されます。
Wear OS では、定数式も動的式とみなされます。たとえば、次のコード スニペットには 4 つの動的式が含まれています。
plus()
オペレーション
animate()
オペレーション
constant(1)
式
constant(2)
式(plus()
動的式の値 2
によって暗黙的に指定)
DynamicInt32.constant(1).plus(2).animate()
あなたへのおすすめ
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Dynamic expressions in Wear OS\n\nWear OS supports dynamic updates to information that appears in your [tiles](/training/wearables/tiles)\nand [complications](/training/wearables/tiles/complications).\n\nUsing dynamic expressions, you can bind data that appears on a surface of your\napp--such as a tile or complication--to a particular data source. An example of\nsuch a data source is heart rate data that the platform can read. After you've\nestablished this binding, the system updates the data in your tiles and\ncomplications automatically.\n\nCreate dynamic data bindings\n----------------------------\n\nTo create a dynamic data binding, define a variable that uses a\n[dynamic data type](#data-types). Associate this variable with the data stream that you\nwant to use.\n\nFor example, you can fetch values related to the system clock and health\ninformation, as shown in the following code snippet.\n| **Note:** To access the data in [`PlatformHealthSources`](/reference/androidx/wear/protolayout/expression/PlatformHealthSources), you must [request a\nruntime permission](/training/permissions/requesting) in your app. \n\n### Kotlin\n\n```kotlin\nval systemTime = DynamicInstant.platformTimeWithSecondsPrecision()\nval steps: DynamicInt32 = PlatformHealthSources.dailySteps()\n```\n\n### Java\n\n```java\nDynamicInstant systemTime = DynamicInstant.platformTimeWithSecondsPrecision();\nDynamicInt32 steps = PlatformHealthSources.dailySteps();\n```\n\nYou can also create dynamic values from constant expressions and perform\narithmetic operations on any dynamic value, as shown in the following snippet: \n\n### Kotlin\n\n```kotlin\nval dynamicValue = DynamicInt32.constant(1).plus(2)\n```\n\n### Java\n\n```java\nDynamicInt32 dynamicValue = DynamicInt32.constant(1).plus(2)\n```\n\n### List of possible dynamic data types\n\nWear OS supports the following dynamic data types:\n\n- [`DynamicBool`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicBool)\n- [`DynamicColor`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicColor)\n- [`DynamicDuration`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicDuration)\n- [`DynamicFloat`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicFloat)\n- [`DynamicInstant`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant)\n- [`DynamicInt32`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInt32)\n- [`DynamicString`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString)\n\nIn addition, you can transform the data type using built-in capabilities, such\nas the following:\n\n- `DynamicInt32` supports conversion to a `DynamicString` using [`format()`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInt32#format()).\n- `DynamicDuration` lets you extract specific parts, such as the seconds part of a duration, as `DynamicInt32` objects.\n\nUse a limited number of dynamic expressions on each screen\n----------------------------------------------------------\n\nThe system has a limit on the number of dynamic expressions that it can process\nsimultaneously on a particular screen. The system converts any additional\ndynamic expressions to static values.\n\nWear OS considers constant expressions to be dynamic expressions, too. For\nexample, the following code snippet contains 4 dynamic expressions:\n\n1. The `plus()` operation.\n2. The `animate()` operation.\n3. The `constant(1)` expression.\n4. The `constant(2)` expression, which is implied by the value `2` in the `plus()` dynamic expression.\n\n DynamicInt32.constant(1).plus(2).animate()\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Migrate to ProtoLayout namespaces](/training/wearables/tiles/migrate-to-protolayout)\n- [Side-effects in Compose](/develop/ui/compose/side-effects)\n- [AGSL Quick Reference](/develop/ui/views/graphics/agsl/agsl-quick-reference)"]]