1# Class (Brush) 2 3<!--Kit: ArkGraphics 2D--> 4<!--Subsystem: Graphics--> 5<!--Owner: @hangmengxin--> 6<!--Designer: @wangyanglan--> 7<!--Tester: @nobuggers--> 8<!--Adviser: @ge-yafang--> 9 10> **说明:** 11> 12> - 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 13> 14> - 本模块使用屏幕物理像素单位px。 15> 16> - 本模块为单线程模型策略,需要调用方自行管理线程安全和上下文状态的切换。 17 18画刷对象,描述所绘制图形的填充信息。 19 20## 导入模块 21 22```ts 23import { drawing } from '@kit.ArkGraphics2D'; 24``` 25 26## constructor<sup>12+</sup> 27 28constructor() 29 30构造一个新的画刷对象。 31 32**系统能力:** SystemCapability.Graphics.Drawing 33 34**示例:** 35 36```ts 37import { drawing } from '@kit.ArkGraphics2D'; 38 39const brush = new drawing.Brush(); 40``` 41 42## constructor<sup>12+</sup> 43 44constructor(brush: Brush) 45 46复制构造一个新的画刷对象。 47 48**系统能力:** SystemCapability.Graphics.Drawing 49 50**参数:** 51 52| 参数名 | 类型 | 必填 | 说明 | 53| ------| ----------- | ---- | ---------------- | 54| brush | [Brush](arkts-apis-graphics-drawing-Brush.md) | 是 | 待复制的画刷对象。 | 55 56**错误码:** 57 58以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 59 60| 错误码ID | 错误信息 | 61| ------- | --------------------------------------------| 62| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 63 64**示例:** 65 66```ts 67import { common2D, drawing } from '@kit.ArkGraphics2D'; 68 69const brush = new drawing.Brush(); 70const brushColor: common2D.Color = { alpha: 255, red: 0, green: 255, blue: 0 }; 71brush.setColor(brushColor); 72const newBrush = new drawing.Brush(brush); 73``` 74 75## setColor 76 77setColor(color: common2D.Color) : void 78 79设置画刷的颜色。 80 81**系统能力:** SystemCapability.Graphics.Drawing 82 83**参数:** 84 85| 参数名 | 类型 | 必填 | 说明 | 86| ------ | ---------------------------------------------------- | ---- | ---------------- | 87| color | [common2D.Color](js-apis-graphics-common2D.md#color) | 是 | ARGB格式的颜色,每个颜色通道的值是0到255之间的整数。 | 88 89**错误码:** 90 91以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 92 93| 错误码ID | 错误信息 | 94| ------- | --------------------------------------------| 95| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 96 97**示例:** 98 99```ts 100import { common2D, drawing } from '@kit.ArkGraphics2D'; 101 102const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 }; 103const brush = new drawing.Brush(); 104brush.setColor(color); 105``` 106 107## setColor<sup>12+</sup> 108 109setColor(alpha: number, red: number, green: number, blue: number): void 110 111设置画刷的颜色。性能优于[setColor](#setcolor)接口,推荐使用本接口。 112 113**系统能力:** SystemCapability.Graphics.Drawing 114 115**参数:** 116 117| 参数名 | 类型 | 必填 | 说明 | 118| ------ | ------ | ---- | -------------------------------------------------- | 119| alpha | number | 是 | ARGB格式颜色的透明度通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 120| red | number | 是 | ARGB格式颜色的红色通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 121| green | number | 是 | ARGB格式颜色的绿色通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 122| blue | number | 是 | ARGB格式颜色的蓝色通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 123 124**错误码:** 125 126以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 127 128| 错误码ID | 错误信息 | 129| ------- | --------------------------------------------| 130| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 131 132**示例:** 133 134```ts 135import { drawing } from '@kit.ArkGraphics2D'; 136 137const brush = new drawing.Brush(); 138brush.setColor(255, 255, 0, 0); 139``` 140 141## setColor<sup>18+</sup> 142 143setColor(color: number) : void 144 145设置画刷的颜色。 146 147**系统能力:** SystemCapability.Graphics.Drawing 148 149**参数:** 150 151| 参数名 | 类型 | 必填 | 说明 | 152| ------ | ---------------------------------------------------- | ---- | ---------------- | 153| color | number | 是 | 16进制ARGB格式的颜色。 | 154 155**错误码:** 156 157以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 158 159| 错误码ID | 错误信息 | 160| ------- | --------------------------------------------| 161| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 162 163**示例:** 164 165```ts 166import { drawing } from '@kit.ArkGraphics2D'; 167 168const brush = new drawing.Brush(); 169brush.setColor(0xffff0000); 170``` 171 172## setColor4f<sup>20+</sup> 173 174setColor4f(color4f: common2D.Color4f, colorSpace: colorSpaceManager.ColorSpaceManager | null): void 175 176设置画刷的颜色以及标准色域,与[setColor](#setcolor)区别在于可以单独设置色域,适用于需要单独设置色域的场景。 177 178**系统能力:** SystemCapability.Graphics.Drawing 179 180**参数:** 181 182| 参数名 | 类型 | 必填 | 说明 | 183| ------ | ---------------------------------------------------- | ---- | ---------------- | 184| color4f | [common2D.Color4f](js-apis-graphics-common2D.md#color4f20) | 是 | ARGB格式的颜色,每个颜色通道的值是0.0-1.0之间的浮点数,大于1.0时,取1.0,小于0.0时,取0.0。| 185| colorSpace | [colorSpaceManager.ColorSpaceManager](js-apis-colorSpaceManager.md#colorspacemanager) \| null | 是 | 标准色域对象,null表示使用SRGB色域。| 186 187**示例:** 188 189```ts 190import { common2D, drawing, colorSpaceManager } from "@kit.ArkGraphics2D"; 191 192const brush = new drawing.Brush(); 193let colorSpace = colorSpaceManager.create(colorSpaceManager.ColorSpace.BT2020_HLG); 194let color4f:common2D.Color4f = {alpha:1, red:0.5, green:0.4, blue:0.7}; 195brush.setColor4f(color4f, colorSpace); 196``` 197 198## getColor<sup>12+</sup> 199 200getColor(): common2D.Color 201 202获取画刷的颜色。 203 204**系统能力:** SystemCapability.Graphics.Drawing 205 206**返回值:** 207 208| 类型 | 说明 | 209| -------------- | -------------- | 210| common2D.Color | 返回画刷的颜色。 | 211 212**示例:** 213 214```ts 215import { common2D, drawing } from '@kit.ArkGraphics2D'; 216 217const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 }; 218const brush = new drawing.Brush(); 219brush.setColor(color); 220let colorGet = brush.getColor(); 221``` 222 223## getColor4f<sup>20+</sup> 224 225getColor4f(): common2D.Color4f 226 227获取画刷的颜色,与[getColor](#getcolor12)的区别是返回值类型为浮点数,适用于需要浮点数类型的场景。 228 229**系统能力:** SystemCapability.Graphics.Drawing 230 231**返回值:** 232 233| 类型 | 说明 | 234| -------------- | -------------- | 235| [common2D.Color4f](js-apis-graphics-common2D.md#color4f20) | 返回画刷的颜色。 | 236 237**示例:** 238 239```ts 240import { common2D, drawing, colorSpaceManager } from "@kit.ArkGraphics2D"; 241 242const brush = new drawing.Brush(); 243let colorSpace = colorSpaceManager.create(colorSpaceManager.ColorSpace.BT2020_HLG); 244let color4f:common2D.Color4f = {alpha:1, red:0.5, green:0.4, blue:0.7}; 245brush.setColor4f(color4f, colorSpace); 246let color = brush.getColor4f(); 247``` 248 249## getHexColor<sup>18+</sup> 250 251getHexColor(): number 252 253获取画刷的颜色。 254 255**系统能力:** SystemCapability.Graphics.Drawing 256 257**返回值:** 258 259| 类型 | 说明 | 260| -------------- | -------------- | 261| number | 返回画刷的颜色,以16进制ARGB格式的32位无符号整数表示。 | 262 263**示例:** 264 265```ts 266import { common2D, drawing } from '@kit.ArkGraphics2D'; 267 268let color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 }; 269let brush = new drawing.Brush(); 270brush.setColor(color); 271let hex_color: number = brush.getHexColor(); 272console.info('getHexColor: ', hex_color.toString(16)); 273``` 274 275## setAntiAlias 276 277setAntiAlias(aa: boolean) : void 278 279设置画刷是否开启抗锯齿。开启后,可以使得图形的边缘在显示时更平滑。未调用此接口设置时,系统默认关闭抗锯齿。 280 281**系统能力:** SystemCapability.Graphics.Drawing 282 283**参数:** 284 285| 参数名 | 类型 | 必填 | 说明 | 286| ------ | ------- | ---- | ------------------------------------------------- | 287| aa | boolean | 是 | 表示是否开启抗锯齿,true表示开启,false表示关闭。 | 288 289**错误码:** 290 291以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 292 293| 错误码ID | 错误信息 | 294| ------- | --------------------------------------------| 295| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 296 297**示例:** 298 299```ts 300import { drawing } from '@kit.ArkGraphics2D'; 301 302const brush = new drawing.Brush(); 303brush.setAntiAlias(true); 304``` 305 306## isAntiAlias<sup>12+</sup> 307 308isAntiAlias(): boolean 309 310获取画刷是否开启抗锯齿属性。 311 312**系统能力:** SystemCapability.Graphics.Drawing 313 314**返回值:** 315 316| 类型 | 说明 | 317| ------- | ------------------------- | 318| boolean | 返回画刷是否开启抗锯齿属性,true表示开启,false表示关闭。 | 319 320**示例:** 321 322```ts 323import { drawing } from '@kit.ArkGraphics2D'; 324 325const brush = new drawing.Brush(); 326let isAntiAlias = brush.isAntiAlias(); 327``` 328 329## setAlpha 330 331setAlpha(alpha: number) : void 332 333设置画刷的透明度。 334 335**系统能力:** SystemCapability.Graphics.Drawing 336 337**参数:** 338 339| 参数名 | 类型 | 必填 | 说明 | 340| ------ | ------ | ---- | ---------------------------------------- | 341| alpha | number | 是 | 用于表示透明度的[0, 255]区间内的整数值,传入浮点类型时向下取整。 | 342 343**错误码:** 344 345以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 346 347| 错误码ID | 错误信息 | 348| ------- | --------------------------------------------| 349| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 350 351**示例:** 352 353```ts 354import { drawing } from '@kit.ArkGraphics2D'; 355 356const brush = new drawing.Brush(); 357brush.setAlpha(128); 358``` 359 360## getAlpha<sup>12+</sup> 361 362getAlpha(): number 363 364获取画刷的透明度。 365 366**系统能力:** SystemCapability.Graphics.Drawing 367 368**返回值:** 369 370| 类型 | 说明 | 371| ------ | ---------------- | 372| number | 返回画刷的透明度,该返回值为0到255之间的整数。 | 373 374**示例:** 375 376```ts 377import { drawing } from '@kit.ArkGraphics2D'; 378 379const brush = new drawing.Brush(); 380let alpha = brush.getAlpha(); 381``` 382 383## setColorFilter 384 385setColorFilter(filter: ColorFilter) : void 386 387给画刷添加额外的颜色滤波器。 388 389**系统能力:** SystemCapability.Graphics.Drawing 390 391**参数:** 392 393| 参数名 | 类型 | 必填 | 说明 | 394| ------ | --------------------------- | ---- | ------------ | 395| filter | [ColorFilter](arkts-apis-graphics-drawing-ColorFilter.md) | 是 | 颜色滤波器。null表示清空颜色滤波器。 | 396 397**错误码:** 398 399以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 400 401| 错误码ID | 错误信息 | 402| ------- | --------------------------------------------| 403| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 404 405**示例:** 406 407```ts 408import { drawing } from '@kit.ArkGraphics2D'; 409 410const brush = new drawing.Brush(); 411let colorFilter = drawing.ColorFilter.createLinearToSRGBGamma(); 412brush.setColorFilter(colorFilter); 413``` 414 415## setMaskFilter<sup>12+</sup> 416 417setMaskFilter(filter: MaskFilter): void 418 419给画刷添加额外的蒙版滤镜。 420 421**系统能力:** SystemCapability.Graphics.Drawing 422 423**参数:** 424 425| 参数名 | 类型 | 必填 | 说明 | 426| ------ | ------------------------- | ---- | --------- | 427| filter | [MaskFilter](arkts-apis-graphics-drawing-MaskFilter.md) | 是 | 蒙版滤镜。null表示清空蒙版滤镜。 | 428 429**错误码:** 430 431以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 432 433| 错误码ID | 错误信息 | 434| ------- | --------------------------------------------| 435| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 436 437**示例:** 438 439```ts 440import { RenderNode } from '@kit.ArkUI'; 441import { common2D, drawing } from '@kit.ArkGraphics2D'; 442 443class DrawingRenderNode extends RenderNode { 444 draw(context : DrawContext) { 445 const canvas = context.canvas; 446 const brush = new drawing.Brush(); 447 let maskFilter = drawing.MaskFilter.createBlurMaskFilter(drawing.BlurType.OUTER, 10); 448 brush.setMaskFilter(maskFilter); 449 } 450} 451``` 452 453## setShaderEffect<sup>12+</sup> 454 455setShaderEffect(shaderEffect: ShaderEffect): void 456 457设置画刷着色器效果。 458 459**系统能力:** SystemCapability.Graphics.Drawing 460 461**参数:** 462 463| 参数名 | 类型 | 必填 | 说明 | 464| ------- | ------------------------- | ---- | ------------ | 465| shaderEffect | [ShaderEffect](arkts-apis-graphics-drawing-ShaderEffect.md) | 是 | 着色器对象。null表示清空着色器效果。 | 466 467**错误码:** 468 469以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 470 471| 错误码ID | 错误信息 | 472| ------- | --------------------------------------------| 473| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 474 475**示例:** 476 477```ts 478import { drawing } from '@kit.ArkGraphics2D'; 479 480const brush = new drawing.Brush(); 481let shaderEffect = drawing.ShaderEffect.createLinearGradient({x: 100, y: 100}, {x: 300, y: 300}, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT); 482brush.setShaderEffect(shaderEffect); 483``` 484 485## setShadowLayer<sup>12+</sup> 486 487setShadowLayer(shadowLayer: ShadowLayer): void 488 489设置画刷阴影层效果。当前仅在绘制文字时生效。 490 491**系统能力:** SystemCapability.Graphics.Drawing 492 493**参数:** 494 495| 参数名 | 类型 | 必填 | 说明 | 496| ------- | ------------------------- | ---- | --------- | 497| shadowLayer | [ShadowLayer](arkts-apis-graphics-drawing-ShadowLayer.md) | 是 | 阴影层对象。null表示清空阴影层效果。 | 498 499**错误码:** 500 501以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 502 503| 错误码ID | 错误信息 | 504| ------- | --------------------------------------------| 505| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 506 507**示例:** 508 509```ts 510import { RenderNode } from '@kit.ArkUI'; 511import { common2D, drawing } from '@kit.ArkGraphics2D'; 512 513class DrawingRenderNode extends RenderNode { 514 draw(context : DrawContext) { 515 const canvas = context.canvas; 516 let font = new drawing.Font(); 517 font.setSize(60); 518 519 let textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8); 520 let pen = new drawing.Pen(); 521 pen.setStrokeWidth(2.0); 522 523 let pen_color : common2D.Color = {alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00}; 524 pen.setColor(pen_color); 525 canvas.attachPen(pen); 526 canvas.drawTextBlob(textBlob, 100, 100); 527 canvas.detachPen(); 528 529 let color : common2D.Color = {alpha: 0xFF, red: 0x00, green: 0xFF, blue: 0x00}; 530 let shadowLayer = drawing.ShadowLayer.create(3, -3, 3, color); 531 pen.setShadowLayer(shadowLayer); 532 canvas.attachPen(pen); 533 canvas.drawTextBlob(textBlob, 100, 200); 534 canvas.detachPen(); 535 536 let brush = new drawing.Brush(); 537 let brush_color : common2D.Color = {alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00}; 538 brush.setColor(brush_color); 539 canvas.attachBrush(brush); 540 canvas.drawTextBlob(textBlob, 300, 100); 541 canvas.detachBrush(); 542 543 brush.setShadowLayer(shadowLayer); 544 canvas.attachBrush(brush); 545 canvas.drawTextBlob(textBlob, 300, 200); 546 canvas.detachBrush(); 547 } 548} 549``` 550 551## setBlendMode 552 553setBlendMode(mode: BlendMode) : void 554 555设置画刷的混合模式。未调用此接口设置时,系统默认的混合模式为SRC_OVER。 556 557**系统能力:** SystemCapability.Graphics.Drawing 558 559**参数:** 560 561| 参数名 | 类型 | 必填 | 说明 | 562| ------ | ----------------------- | ---- | ---------------- | 563| mode | [BlendMode](arkts-apis-graphics-drawing-e.md#blendmode) | 是 | 颜色的混合模式。 | 564 565**错误码:** 566 567以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 568 569| 错误码ID | 错误信息 | 570| ------- | --------------------------------------------| 571| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 572 573**示例:** 574 575```ts 576import { drawing } from '@kit.ArkGraphics2D'; 577 578const brush = new drawing.Brush(); 579brush.setBlendMode(drawing.BlendMode.SRC); 580``` 581 582## setImageFilter<sup>12+</sup> 583 584setImageFilter(filter: ImageFilter | null): void 585 586为画刷设置图像滤波器。 587 588**系统能力:** SystemCapability.Graphics.Drawing 589 590**参数:** 591 592| 参数名 | 类型 | 必填 | 说明 | 593| ------ | ------ | ---- | ----------------------- | 594| filter | [ImageFilter](arkts-apis-graphics-drawing-ImageFilter.md) \| null | 是 | 图像滤波器,null表示清空图像滤波器效果。 | 595 596**错误码:** 597 598以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 599 600| 错误码ID | 错误信息 | 601| ------- | --------------------------------------------| 602| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types. | 603 604**示例:** 605 606```ts 607import {drawing} from '@kit.ArkGraphics2D'; 608 609let brush = new drawing.Brush(); 610let imgFilter = drawing.ImageFilter.createBlurImageFilter(5, 10, drawing.TileMode.DECAL); 611brush.setImageFilter(imgFilter); 612brush.setImageFilter(null); 613``` 614 615## getColorFilter<sup>12+</sup> 616 617getColorFilter(): ColorFilter 618 619获取画刷的颜色滤波器。 620 621**系统能力:** SystemCapability.Graphics.Drawing 622 623**返回值:** 624 625| 类型 | 说明 | 626| --------------------------- | ------------------ | 627| [ColorFilter](arkts-apis-graphics-drawing-ColorFilter.md) | 返回颜色滤波器。 | 628 629**示例:** 630 631```ts 632import {drawing} from '@kit.ArkGraphics2D'; 633 634let brush = new drawing.Brush(); 635let setColorFilter = drawing.ColorFilter.createSRGBGammaToLinear(); 636brush.setColorFilter(setColorFilter); 637let filter = brush.getColorFilter(); 638``` 639 640## reset<sup>12+</sup> 641 642reset(): void 643 644重置当前画刷为初始状态。 645 646**系统能力:** SystemCapability.Graphics.Drawing 647 648**示例:** 649 650```ts 651import { drawing } from '@kit.ArkGraphics2D'; 652 653const brush = new drawing.Brush(); 654brush.reset(); 655```