漸變對象

2024-01-22 18:10 更新

漸變對象。

說明

從 API Version 8 開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨標(biāo)記該內(nèi)容的起始版本。

addColorStop

addColorStop(offset: number, color: string): void

設(shè)置漸變斷點值,包括偏移和顏色。

從API version 9開始,該接口支持在ArkTS卡片中使用。

參數(shù):

參數(shù)

類型

必填

默認(rèn)值

描述

offset

number

0

設(shè)置漸變點距離起點的位置占總體長度的比例,范圍為0到1。

color

string

'#ffffff'

設(shè)置漸變的顏色。顏色格式參考ResourceColor中string類型說明。

示例:

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct Page45 {
  5. private settings: RenderingContextSettings = new RenderingContextSettings(true)
  6. private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  7. build() {
  8. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  9. Canvas(this.context)
  10. .width('100%')
  11. .height('100%')
  12. .backgroundColor('#ffff00')
  13. .onReady(() => {
  14. var grad = this.context.createLinearGradient(50, 0, 300, 100)
  15. grad.addColorStop(0.0, '#ff0000')
  16. grad.addColorStop(0.5, '#ffffff')
  17. grad.addColorStop(1.0, '#00ff00')
  18. this.context.fillStyle = grad
  19. this.context.fillRect(0, 0, 400, 400)
  20. })
  21. }
  22. .width('100%')
  23. .height('100%')
  24. }
  25. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號