使用 Jetpack 卡片库在 Wear OS by Google 谷歌上创建自定义卡片
The following article is from Android 开发者 Author Android
作者 / Jolanda Verhoef,开发者关系工程师
在 2019 年推出了卡片
https://blog.google/products/wear-os/tiles-wear-os-google/
Jetpack 卡片库
https://developer.android.google.cn/training/articles/wear-tiles
开始构建
dependencies {
implementation "androidx.wear:wear-tiles:1.0.0-alpha01"
debugImplementation "androidx.wear:wear-tiles-renderer:1.0.0-alpha01"
}
class MyTileService : TileProviderService() {
override fun onTileRequest(requestParams: RequestReaders.TileRequest) =
Futures.immediateFuture(Tile.builder()
.setResourcesVersion("1")
.setTimeline(Timeline.builder().addTimelineEntry(
// For more information about timelines, see the docs
TimelineEntry.builder().setLayout(
Layout.builder().setRoot(
Text.builder().setText("Hello world!")
)
)
)
).build())
override fun onResourcesRequest(requestParams: ResourcesRequest) =
Futures.immediateFuture(Resources.builder()
.setVersion("1")
.build()
)
}
TileProviderService
https://developer.android.google.cn/reference/androidx/wear/tiles/TileProviderService
这段代码有两个重要部分:
onTileRequest() 用于创建卡片布局。您将在此函数内编写大部分代码。您可以使用多个 TimelineEntry 实例来为不同的时间点渲染不同的布局。
onResourcesRequest() 则用于传递渲染卡片所需的所有资源。如果您决定添加图形,则可以在此处进行添加。
TimelineEntry
https://developer.android.google.cn/reference/androidx/wear/tiles/builders/TimelineBuilders.TimelineEntry不同的时间点
https://developer.android.google.cn/training/articles/wear-tiles#timebound-timeline
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rootLayout = findViewById<FrameLayout>(R.id.tile_container)
TileManager(
context = this,
component = ComponentName(this, MyTileService::class.java),
parentView = rootLayout
).create()
}
}
新发布的指南
https://developer.android.google.cn/training/articles/wear-tiles卡片示例
https://github.com/android/wear-os-samples/tree/main/WearTilesKotlin
提交您的反馈
https://issuetracker.google.com/issues/new?component=460965&template=1422653
推荐阅读