如何为WearOS SmallImage Complication添加常显模式图片?
解决方案:为WearOS Complication添加常显模式图片
要实现正常模式显示img1.png、常显(ambient)模式显示outlines.png,核心是利用SmallImageComplicationData.Builder提供的setAmbientSmallImage()方法,专门为常显模式指定独立的图片资源。
修改onComplicationRequest函数
在构建SmallImageComplicationData时,在原有代码基础上添加常显图片的配置:
return when (request.complicationType) { ComplicationType.SMALL_IMAGE -> SmallImageComplicationData.Builder( smallImage = SmallImage.Builder( Icon.createWithResource(this, R.drawable.img1), SmallImageType.PHOTO ).build(), contentDescription = PlainComplicationText.Builder(text = "Sample text").build() ) // 添加这一行:设置常显模式下的图片 .setAmbientSmallImage( SmallImage.Builder( Icon.createWithResource(this, R.drawable.outlines), SmallImageType.PHOTO ).build() ) .setTapAction(complicationPendingIntent) .build() else -> { if (Log.isLoggable(TAG, Log.WARN)) { Log.w(TAG, "Unexpected complication type ${request.complicationType}") } null } }
关于getPreviewData函数
预览数据主要用于在表盘选择界面展示Complication的正常状态效果,通常不需要配置常显模式的图片,保持原有代码即可。如果需要在预览中模拟常显效果,也可以添加同样的setAmbientSmallImage()调用,但一般没必要。
内容的提问来源于stack exchange,提问作者Marvil
相关产品推荐
相关产品推荐

