繪製錶面

設定專案並新增實作錶面服務的類別後,就可以開始編寫程式碼,初始化及繪製自訂錶面。

每個錶面都會建立轉譯器的自訂子類別,實作繪製錶面所需的所有內容。

轉譯器轉譯錶面時,會結合 UserStyleComplicationSlotsManager 提供的小工具資訊、目前時間和其他狀態資訊,如以下範例所示:

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
    }
}