設定專案並新增實作錶面服務的類別後,您便可以編寫程式碼,以便初始化並繪製自訂錶面了。
每個錶面都會建立自訂的轉譯器子類別,可實作所有繪製錶面所需的內容。
轉譯器會使用 UserStyle
、ComplicationSlotsManager
提供的小工具資訊、目前時間,以及其他狀態資訊,藉此轉譯錶面,如以下範例所示
class CustomCanvasRenderer(
private val context: Context,
surfaceHolder: SurfaceHolder,
watchState: WatchState,
private val complicationSlotsManager: ComplicationSlotsManager,
currentUserStyleRepository: CurrentUserStyleRepository,
canvasType: Int
) : Renderer.CanvasRenderer(
surfaceHolder = surfaceHolder,
currentUserStyleRepository = currentUserStyleRepository,
watchState = watchState,
canvasType = canvasType,
interactiveDrawModeUpdateDelayMillis = 16L
) {
override fun render(canvas: Canvas, bounds: Rect, zonedDateTime: ZonedDateTime) {
// Draw into the canvas!
}
override fun renderHighlightLayer(canvas: Canvas, bounds: Rect, zonedDateTime: ZonedDateTime) {
// Draw into the canvas!
}
}