실제 크기(색상을 제외한 카드의 모든 값)에 영향을 주는 동적 표현식을 사용할 때는 문자열 형식과 같은 관련 제약 조건도 지정해야 합니다. 이러한 제약 조건을 사용하면 시스템 렌더기가 카드 내에서 값이 차지할 수 있는 최대 공간을 확인할 수 있습니다. 일반적으로 이러한 제약 조건은 setLayoutConstraintsForDynamic*으로 시작하는 메서드를 호출하여 동적 표현식 수준이 아닌 요소 수준에서 지정해야 합니다.
다음 코드 스니펫은 3자리 숫자와 대체 값 --를 사용하여 심박수 업데이트를 표시하는 방법을 보여줍니다.
onTileRequest() 구현에서 setState()를 호출하고 각 키에서 특정 동적 데이터 값으로의 초기 매핑을 설정합니다.
Kotlin
overridefunonTileRequest(requestParams:TileRequest):ListenableFuture<Tile>{valstate=State.Builder().addKeyToValueMapping(KEY_WATER_INTAKE,DynamicDataBuilders.DynamicDataValue.fromInt(200)).addKeyToValueMapping(KEY_NOTE,DynamicDataBuilders.DynamicDataValue.fromString("Note about day")).build()// ...returnFutures.immediateFuture(Tile.Builder()// Set resources, timeline, and other tile properties..setState(state).build())
Java
@OverrideprotectedListenableFuture<Tile>onTileRequest(ListenableFuture<Tile>{Statestate=newState.Builder().addKeyToValueMapping(KEY_WATER_INTAKE,DynamicDataBuilders.DynamicDataValue.fromInt(200)).addKeyToValueMapping(KEY_NOTE,DynamicDataBuilders.DynamicDataValue.fromString("Note about day")).build();// ...returnFutures.immediateFuture(Tile.Builder()// Set resources, timeline, and other tile properties..setState(state).build());}
레이아웃을 만들 때는 상태의 데이터를 표시할 위치에서 Dynamic* 유형 객체를 사용합니다. animate()를 호출하여 이전 값에서 현재 값으로의 애니메이션을 보여줄 수도 있습니다.
Kotlin
DynamicInt32.from(KEY_WATER_INTAKE).animate()
Java
DynamicInt32.from(KEY_WATER_INTAKE).animate();
필요한 경우 상태를 새 값으로 업데이트할 수도 있습니다. 이는 카드의 LoadAction에 포함할 수 있습니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 Oracle 계열사의 상표 또는 등록 상표입니다.
최종 업데이트: 2025-08-27(UTC)
[null,null,["최종 업데이트: 2025-08-27(UTC)"],[],[],null,["Starting in Tiles 1.2, you can stream platform data updates using\n[dynamic expressions](/training/wearables/data/dynamic). You can then associate these updates with animations\nin your tiles. Your app gets updates to this value every second.\n\nUsing dynamic expressions, you don't need to refresh the entire tile when its\ncontent changes. To create a more engaging experience in your tiles, animate\nthose dynamic objects.\n\nAssociate dynamic expressions with data sources\n\nThe `androidx.wear.protolayout` and `androidx.wear.protolayout.material`\nnamespaces contain many classes whose fields accept dynamic expressions.\nSeveral examples include the following:\n\n- Several length values, including the [length of an `Arc` object](/reference/androidx/wear/protolayout/LayoutElementBuilders.ArcLine#getLength()) and the [length of a `CircularProgressIndicator`](/reference/androidx/wear/protolayout/material/CircularProgressIndicator#getProgress()) object.\n- Any color, such as the [content color of a `Button` object](/reference/androidx/wear/protolayout/material/ButtonColors#getContentColor()).\n- Many string values, including the [content of a `Text` object](/reference/androidx/wear/protolayout/material/Text#getText()), the [content of a `LayoutElementsBuilders.Text` object](/reference/androidx/wear/protolayout/LayoutElementBuilders.Text#getText()), and the [content\n description of a `CircularProgressIndicator` object](/reference/androidx/wear/protolayout/material/CircularProgressIndicator#getContentDescription()).\n\nTo use a dynamic expression as a possible value for an element in your tile, use\nthe element's corresponding `*Prop` dynamic property type and pass in the data\nsource to the dynamic property type's builder class's `setDynamicValue()`\nmethod.\n\nTiles support these dynamic property types:\n\n- For linear dimensions, measured in display-independent pixels, use [`DimensionBuilders.DpProp`](/reference/androidx/wear/tiles/DimensionBuilders.DpProp).\n- For angular dimensions, measured in degrees, use [`DimensionBuilders.DegreesProp`](/reference/androidx/wear/tiles/DimensionBuilders.DegreesProp).\n- For string values, use [`TypeBuilders.StringProp`](/reference/androidx/wear/protolayout/TypeBuilders.StringProp).\n- For color values, use [`ColorBuilders.ColorProp`](/reference/androidx/wear/protolayout/ColorBuilders.ColorProp).\n- For floating-point values, use [`TypeBuilders.FloatProp`](/reference/androidx/wear/protolayout/TypeBuilders.FloatProp).\n\nWhen you use a dynamic expression that affects physical dimensions---any value in\na tile except for color---you must also specify a set of related constraints, such\nas a string format. These constraints allow the system renderer to determine the\nmaximum amount of space that a value could occupy within your tile. Usually, you\nspecify these constraints at the element level, not at the dynamic expression\nlevel, by calling a method that starts with `setLayoutConstraintsForDynamic*`.\n| **Note:** The Material components set these layout constraints automatically.\n\nThe following code snippet shows how to display updates to a heart rate using 3\ndigits, with a fallback value of `--`: \n\n```kotlin\noverride fun onTileRequest(requestParams: RequestBuilders.TileRequest) =\n Futures.immediateFuture(\n Tile.Builder()\n .setResourcesVersion(RESOURCES_VERSION)\n .setFreshnessIntervalMillis(60 * 60 * 1000) // 60 minutes\n .setTileTimeline(\n Timeline.fromLayoutElement(\n Text.Builder(\n this,\n TypeBuilders.StringProp.Builder(\"--\")\n .setDynamicValue(\n PlatformHealthSources.heartRateBpm()\n .format()\n .concat(DynamicBuilders.DynamicString.constant(\" bpm\"))\n )\n .build(),\n TypeBuilders.StringLayoutConstraint.Builder(\"000\").build(),\n )\n .build()\n )\n )\n .build()\n )https://github.com/android/snippets/blob/5673ffc60b614daf028ee936227128eb8c4f9781/wear/src/main/java/com/example/wear/snippets/tile/Tile.kt#L178-L200\n```\n\nUse a small number of expressions within a single tile\n\nWear OS [places a limit](/training/wearables/data/dynamic#use-limited-number-per-screen) on the number of expressions that a single tile can\nhave. If a tile contains too many total dynamic expressions, dynamic values are\nignored, and the system falls back to the static values that you provide to the\nrespective dynamic property types.\n\nConsolidate dynamic data into a state object\n\nYou can consolidate the latest set of updates from data sources into a *state*,\nwhich you pass over to your tile for value rendering.\n\nTo use state information in your tiles, complete these steps:\n\n1. Establish a set of keys that represent the different values of your tile's\n state. This example creates keys for water intake and a note:\n\n Kotlin \n\n ```kotlin\n companion object {\n val KEY_WATER_INTAKE = AppDataKey\u003cDynamicInt32\u003e(\"water_intake\")\n val KEY_NOTE = AppDataKey\u003cDynamicString\u003e(\"note\")\n }\n ```\n\n Java \n\n ```java\n private static final AppDataKey\u003cDynamicInt32\u003e KEY_WATER_INTAKE =\n new AppDataKey\u003cDynamicInt32\u003e(\"water_intake\");\n private static final AppDataKey\u003cDynamicString\u003e KEY_NOTE =\n new AppDataKey\u003cDynamicString\u003e(\"note\");\n ```\n2. In your implementation of `onTileRequest()`, call `setState()` and establish\n initial mappings from each key to a particular dynamic data value:\n\n Kotlin \n\n ```kotlin\n override fun onTileRequest(requestParams: TileRequest):\n ListenableFuture\u003cTile\u003e {\n val state = State.Builder()\n .addKeyToValueMapping(KEY_WATER_INTAKE,\n DynamicDataBuilders.DynamicDataValue.fromInt(200))\n .addKeyToValueMapping(KEY_NOTE,\n DynamicDataBuilders.DynamicDataValue.fromString(\"Note about day\"))\n .build()\n // ...\n\n return Futures.immediateFuture(Tile.Builder()\n // Set resources, timeline, and other tile properties.\n .setState(state)\n .build()\n )\n ```\n\n Java \n\n ```java\n @Override\n protected ListenableFuture\u003cTile\u003e onTileRequest(\n ListenableFuture\u003cTile\u003e {\n State state = new State.Builder()\n .addKeyToValueMapping(KEY_WATER_INTAKE,\n DynamicDataBuilders.DynamicDataValue.fromInt(200))\n .addKeyToValueMapping(KEY_NOTE,\n DynamicDataBuilders.DynamicDataValue.fromString(\"Note about day\"))\n .build();\n // ...\n\n return Futures.immediateFuture(Tile.Builder()\n // Set resources, timeline, and other tile properties.\n .setState(state)\n .build()\n );\n }\n ```\n3. When you create your layout, in a place where you want to show this data\n from state, use a `Dynamic*` type object. You can also call `animate()` to\n show an animation from the previous value to the current value:\n\n Kotlin \n\n ```kotlin\n DynamicInt32.from(KEY_WATER_INTAKE).animate()\n ```\n\n Java \n\n ```java\n DynamicInt32.from(KEY_WATER_INTAKE).animate();\n ```\n4. When needed, you can also update the state with new values. This can be\n part of a tile's [`LoadAction`](/reference/androidx/wear/protolayout/ActionBuilders.LoadAction).\n\n In this example, the water intake value is updated to `400`: \n\n Kotlin \n\n ```kotlin\n val loadAction = LoadAction.Builder()\n .setRequestState(\n State.Builder()\n .addKeyToValueMapping(\n KEY_WATER_INTAKE,\n DynamicDataBuilders.DynamicDataValue.fromInt(400)\n )\n .build()\n )\n .build()\n ```\n\n Java \n\n ```java\n LoadAction loadAction = new LoadAction.Builder()\n .setRequestState(\n new State.Builder()\n .addKeyToValueMapping(\n KEY_WATER_INTAKE,\n DynamicDataBuilders.DynamicDataValue.fromInt(400)\n ).build()\n ).build();\n ```\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Migrate to ProtoLayout namespaces](/training/wearables/tiles/migrate-to-protolayout)\n- [Get started with tiles](/training/wearables/tiles/get_started)\n- [Other considerations](/develop/ui/compose/migrate/other-considerations)"]]