1# Class (Pen) 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 pen = new drawing.Pen(); 40``` 41 42## constructor<sup>12+</sup> 43 44constructor(pen: Pen) 45 46复制构造一个新的画笔对象。 47 48**系统能力:** SystemCapability.Graphics.Drawing 49 50**参数:** 51 52| 参数名 | 类型 | 必填 | 说明 | 53| ------| ----------- | ---- | ---------------- | 54| pen | [Pen](arkts-apis-graphics-drawing-Pen.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 pen = new drawing.Pen(); 70const penColor: common2D.Color = { alpha: 255, red: 0, green: 255, blue: 0 }; 71pen.setColor(penColor); 72pen.setStrokeWidth(10); 73const newPen = new drawing.Pen(pen); 74``` 75 76## setMiterLimit<sup>12+</sup> 77 78setMiterLimit(miter: number): void 79 80设置折线尖角长度与线宽的最大比值,当画笔绘制一条折线,并且[JoinStyle](arkts-apis-graphics-drawing-e.md#joinstyle12)为MITER_JOIN时,若尖角长度与线宽的比值大于限制值,则该折角使用BEVEL_JOIN绘制。 81 82**系统能力:** SystemCapability.Graphics.Drawing 83 84**参数:** 85 86| 参数名 | 类型 | 必填 | 说明 | 87| ------ | ------ | ---- | ---------------- | 88| miter | number | 是 | 折线尖角长度与线宽的最大比值,负数在绘制时会被视作4.0处理,非负数正常生效,该参数为浮点数。 | 89 90**错误码:** 91 92以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 93 94| 错误码ID | 错误信息 | 95| ------- | --------------------------------------------| 96| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 97 98**示例:** 99 100```ts 101import { drawing } from '@kit.ArkGraphics2D'; 102 103const pen = new drawing.Pen(); 104pen.setMiterLimit(5); 105``` 106 107## getMiterLimit<sup>12+</sup> 108 109getMiterLimit(): number 110 111获取折线尖角的限制值。 112 113**系统能力:** SystemCapability.Graphics.Drawing 114 115**返回值:** 116 117| 类型 | 说明 | 118| -------| -------------------- | 119| number | 返回折线尖角长度与线宽的最大比值。 | 120 121**示例:** 122 123```ts 124import { drawing } from '@kit.ArkGraphics2D'; 125 126const pen = new drawing.Pen(); 127let miter = pen.getMiterLimit(); 128``` 129 130## setImageFilter<sup>12+</sup> 131 132setImageFilter(filter: ImageFilter | null): void 133 134设置画笔的图像滤波器。 135 136**系统能力:** SystemCapability.Graphics.Drawing 137 138**参数:** 139 140| 参数名 | 类型 | 必填 | 说明 | 141| ------ | ------ | ---- | ----------------------- | 142| filter | [ImageFilter](arkts-apis-graphics-drawing-ImageFilter.md) \| null | 是 | 图像滤波器,null表示清空画笔的图像滤波器效果。 | 143 144**错误码:** 145 146以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 147 148| 错误码ID | 错误信息 | 149| ------- | --------------------------------------------| 150| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;2. Incorrect parameter types. | 151 152**示例:** 153 154```ts 155import {drawing} from '@kit.ArkGraphics2D'; 156 157let colorfilter = drawing.ColorFilter.createSRGBGammaToLinear(); 158let imgFilter = drawing.ImageFilter.createFromColorFilter(colorfilter); 159let pen = new drawing.Pen(); 160pen.setImageFilter(imgFilter); 161pen.setImageFilter(null); 162``` 163 164## getColorFilter<sup>12+</sup> 165 166getColorFilter(): ColorFilter 167 168获取画笔的颜色滤波器。 169 170**系统能力:** SystemCapability.Graphics.Drawing 171 172**返回值:** 173 174| 类型 | 说明 | 175| --------------------------- | ------------------ | 176| [ColorFilter](arkts-apis-graphics-drawing-ColorFilter.md) | 返回颜色滤波器。 | 177 178**示例:** 179 180```ts 181import {drawing} from '@kit.ArkGraphics2D'; 182 183let pen = new drawing.Pen(); 184let colorfilter = drawing.ColorFilter.createLumaColorFilter(); 185pen.setColorFilter(colorfilter); 186let filter = pen.getColorFilter(); 187``` 188 189## setColor 190 191setColor(color: common2D.Color) : void 192 193设置画笔的颜色。 194 195**系统能力:** SystemCapability.Graphics.Drawing 196 197**参数:** 198 199| 参数名 | 类型 | 必填 | 说明 | 200| ------ | ---------------------------------------------------- | ---- | ---------------- | 201| color | [common2D.Color](js-apis-graphics-common2D.md#color) | 是 | ARGB格式的颜色,每个颜色通道的值是0到255之间的整数。 | 202 203**错误码:** 204 205以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 206 207| 错误码ID | 错误信息 | 208| ------- | --------------------------------------------| 209| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 210 211**示例:** 212 213```ts 214import { common2D, drawing } from '@kit.ArkGraphics2D'; 215 216const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 }; 217const pen = new drawing.Pen(); 218pen.setColor(color); 219``` 220 221## setColor<sup>12+</sup> 222 223setColor(alpha: number, red: number, green: number, blue: number): void 224 225设置画笔的颜色。性能优于[setColor](#setcolor)接口,推荐使用本接口。 226 227**系统能力:** SystemCapability.Graphics.Drawing 228 229**参数:** 230 231| 参数名 | 类型 | 必填 | 说明 | 232| ------ | ------ | ---- | -------------------------------------------------- | 233| alpha | number | 是 | ARGB格式颜色的透明度通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 234| red | number | 是 | ARGB格式颜色的红色通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 235| green | number | 是 | ARGB格式颜色的绿色通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 236| blue | number | 是 | ARGB格式颜色的蓝色通道值,该参数是0到255之间的整数,传入范围内的浮点数会向下取整。 | 237 238**错误码:** 239 240以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 241 242| 错误码ID | 错误信息 | 243| ------- | --------------------------------------------| 244| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 245 246**示例:** 247 248```ts 249import { drawing } from '@kit.ArkGraphics2D'; 250 251const pen = new drawing.Pen(); 252pen.setColor(255, 255, 0, 0); 253``` 254 255## setColor<sup>18+</sup> 256 257setColor(color: number) : void 258 259设置画笔的颜色。 260 261**系统能力:** SystemCapability.Graphics.Drawing 262 263**参数:** 264 265| 参数名 | 类型 | 必填 | 说明 | 266| ------ | ---------------------------------------------------- | ---- | ---------------- | 267| color | number | 是 | 16进制ARGB格式的颜色。 | 268 269**示例:** 270 271```ts 272import { drawing } from '@kit.ArkGraphics2D'; 273 274const pen = new drawing.Pen(); 275pen.setColor(0xffff0000); 276``` 277 278## setColor4f<sup>20+</sup> 279 280setColor4f(color4f: common2D.Color4f, colorSpace: colorSpaceManager.ColorSpaceManager | null): void 281 282设置画笔的颜色以及标准色域,与[setColor](#setcolor)区别在于可以单独设置色域,适用于需要单独设置色域的场景。 283 284**系统能力:** SystemCapability.Graphics.Drawing 285 286**参数:** 287 288| 参数名 | 类型 | 必填 | 说明 | 289| ------ | ---------------------------------------------------- | ---- | ---------------- | 290| color4f | [common2D.Color4f](js-apis-graphics-common2D.md#color4f20) | 是 | ARGB格式的颜色,每个颜色通道的值是0.0-1.0之间的浮点数,大于1.0时,取1.0,小于0.0时,取0.0。| 291| colorSpace | [colorSpaceManager.ColorSpaceManager](js-apis-colorSpaceManager.md#colorspacemanager) \| null | 是 | 标准色域对象,null表示使用SRGB色域。| 292 293**示例:** 294 295```ts 296import { common2D, drawing, colorSpaceManager } from "@kit.ArkGraphics2D"; 297 298const pen = new drawing.Pen(); 299let colorSpace = colorSpaceManager.create(colorSpaceManager.ColorSpace.BT2020_HLG); 300let color4f:common2D.Color4f = {alpha:1, red:0.5, green:0.4, blue:0.7}; 301pen.setColor4f(color4f, colorSpace); 302``` 303 304## getColor<sup>12+</sup> 305 306getColor(): common2D.Color 307 308获取画笔的颜色。 309 310**系统能力:** SystemCapability.Graphics.Drawing 311 312**返回值:** 313 314| 类型 | 说明 | 315| -------------- | -------------- | 316| [common2D.Color](js-apis-graphics-common2D.md#color) | 返回画笔的颜色。 | 317 318**示例:** 319 320```ts 321import { common2D, drawing } from '@kit.ArkGraphics2D'; 322 323const color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 }; 324const pen = new drawing.Pen(); 325pen.setColor(color); 326let colorGet = pen.getColor(); 327``` 328 329## getColor4f<sup>20+</sup> 330 331getColor4f(): common2D.Color4f 332 333获取画笔的颜色,与[getColor](#getcolor12)的区别在于返回值类型为浮点数,适用于需要浮点数类型的场景。 334 335**系统能力:** SystemCapability.Graphics.Drawing 336 337**返回值:** 338 339| 类型 | 说明 | 340| -------------- | -------------- | 341|[common2D.Color4f](js-apis-graphics-common2D.md#color4f20) | 返回画笔的颜色。 | 342 343**示例:** 344 345```ts 346import { common2D, drawing, colorSpaceManager } from "@kit.ArkGraphics2D"; 347 348const pen = new drawing.Pen(); 349let colorSpace = colorSpaceManager.create(colorSpaceManager.ColorSpace.BT2020_HLG); 350let color4f:common2D.Color4f = {alpha:1, red:0.5, green:0.4, blue:0.7}; 351pen.setColor4f(color4f, colorSpace); 352let color = pen.getColor4f(); 353``` 354 355## getHexColor<sup>18+</sup> 356 357getHexColor(): number 358 359获取画笔的颜色。 360 361**系统能力:** SystemCapability.Graphics.Drawing 362 363**返回值:** 364 365| 类型 | 说明 | 366| -------------- | -------------- | 367| number | 返回画笔的颜色,以16进制ARGB格式的32位无符号整数表示。 | 368 369**示例:** 370 371```ts 372import { common2D, drawing } from '@kit.ArkGraphics2D'; 373 374let color : common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 }; 375let pen = new drawing.Pen(); 376pen.setColor(color); 377let hex_color: number = pen.getHexColor(); 378console.info('getHexColor: ', hex_color.toString(16)); 379``` 380 381## setStrokeWidth 382 383setStrokeWidth(width: number) : void 384 385设置画笔的线宽。0线宽被视作特殊的极细线宽,在绘制时始终会被绘制为1像素,不随画布的缩放而改变;负数线宽在实际绘制时会被视作0线宽。 386 387**系统能力:** SystemCapability.Graphics.Drawing 388 389**参数:** 390 391| 参数名 | 类型 | 必填 | 说明 | 392| ------ | ------ | ---- | ---------------- | 393| width | number | 是 | 表示线宽,该参数为浮点数。 | 394 395**错误码:** 396 397以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 398 399| 错误码ID | 错误信息 | 400| ------- | --------------------------------------------| 401| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 402 403**示例:** 404 405```ts 406import { drawing } from '@kit.ArkGraphics2D'; 407 408const pen = new drawing.Pen(); 409pen.setStrokeWidth(5); 410``` 411 412## getWidth<sup>12+</sup> 413 414getWidth(): number 415 416获取画笔的线宽属性,线宽描述了画笔绘制图形轮廓的宽度。 417 418**系统能力:** SystemCapability.Graphics.Drawing 419 420**返回值:** 421 422| 类型 | 说明 | 423| ------ | -------------- | 424| number | 返回画笔的线宽,单位为物理像素px。 | 425 426**示例:** 427 428```ts 429import { drawing } from '@kit.ArkGraphics2D'; 430 431const pen = new drawing.Pen(); 432let width = pen.getWidth(); 433``` 434 435## setAntiAlias 436 437setAntiAlias(aa: boolean) : void 438 439设置画笔是否开启抗锯齿。开启后,可以使得图形的边缘在显示时更平滑。未调用此接口设置时,系统默认关闭抗锯齿。 440 441**系统能力:** SystemCapability.Graphics.Drawing 442 443**参数:** 444 445| 参数名 | 类型 | 必填 | 说明 | 446| ------ | ------- | ---- | ------------------------------------------------- | 447| aa | boolean | 是 | 表示是否开启抗锯齿。true表示开启,false表示关闭。 | 448 449**错误码:** 450 451以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 452 453| 错误码ID | 错误信息 | 454| ------- | --------------------------------------------| 455| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 456 457**示例:** 458 459```ts 460import { drawing } from '@kit.ArkGraphics2D'; 461 462const pen = new drawing.Pen(); 463pen.setAntiAlias(true); 464``` 465 466## isAntiAlias<sup>12+</sup> 467 468isAntiAlias(): boolean 469 470获取画笔是否开启抗锯齿属性。 471 472**系统能力:** SystemCapability.Graphics.Drawing 473 474**返回值:** 475 476| 类型 | 说明 | 477| ------- | ------------------------- | 478| boolean | 返回画笔是否开启抗锯齿属性,true表示开启,false表示关闭。 | 479 480**示例:** 481 482```ts 483import { drawing } from '@kit.ArkGraphics2D'; 484 485const pen = new drawing.Pen(); 486let isAntiAlias = pen.isAntiAlias(); 487``` 488 489## setAlpha 490 491setAlpha(alpha: number) : void 492 493设置画笔的透明度。 494 495**系统能力:** SystemCapability.Graphics.Drawing 496 497**参数:** 498 499| 参数名 | 类型 | 必填 | 说明 | 500| ------ | ------ | ---- | ---------------------------------------- | 501| alpha | number | 是 | 用于表示透明度的[0, 255]区间内的整数值,传入浮点类型时向下取整。 | 502 503**错误码:** 504 505以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 506 507| 错误码ID | 错误信息 | 508| ------- | --------------------------------------------| 509| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 510 511**示例:** 512 513```ts 514import { drawing } from '@kit.ArkGraphics2D'; 515 516const pen = new drawing.Pen(); 517pen.setAlpha(128); 518``` 519 520## getAlpha<sup>12+</sup> 521 522getAlpha(): number 523 524获取画笔的透明度。 525 526**系统能力:** SystemCapability.Graphics.Drawing 527 528**返回值:** 529 530| 类型 | 说明 | 531| ------ | ---------------- | 532| number | 返回画笔的透明度,该返回值为0到255之间的整数。 | 533 534**示例:** 535 536```ts 537import { drawing } from '@kit.ArkGraphics2D'; 538 539const pen = new drawing.Pen(); 540let alpha = pen.getAlpha(); 541``` 542 543## setColorFilter 544 545setColorFilter(filter: ColorFilter) : void 546 547给画笔添加额外的颜色滤波器。 548 549**系统能力:** SystemCapability.Graphics.Drawing 550 551**参数:** 552 553| 参数名 | 类型 | 必填 | 说明 | 554| ------ | --------------------------- | ---- | ------------ | 555| filter | [ColorFilter](arkts-apis-graphics-drawing-ColorFilter.md) | 是 | 颜色滤波器。null表示清空颜色滤波器。 | 556 557**错误码:** 558 559以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 560 561| 错误码ID | 错误信息 | 562| ------- | --------------------------------------------| 563| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 564 565**示例:** 566 567```ts 568import { drawing } from '@kit.ArkGraphics2D'; 569 570const pen = new drawing.Pen(); 571let colorFilter = drawing.ColorFilter.createLinearToSRGBGamma(); 572pen.setColorFilter(colorFilter); 573``` 574 575## setMaskFilter<sup>12+</sup> 576 577setMaskFilter(filter: MaskFilter): void 578 579给画笔添加额外的蒙版滤镜。 580 581**系统能力:** SystemCapability.Graphics.Drawing 582 583**参数:** 584 585| 参数名 | 类型 | 必填 | 说明 | 586| ------ | ------------------------- | ---- | --------- | 587| filter | [MaskFilter](arkts-apis-graphics-drawing-MaskFilter.md) | 是 | 蒙版滤镜。null表示清空蒙版滤镜。 | 588 589**错误码:** 590 591以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 592 593| 错误码ID | 错误信息 | 594| ------- | --------------------------------------------| 595| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 596 597**示例:** 598 599```ts 600import { RenderNode } from '@kit.ArkUI'; 601import { common2D, drawing } from '@kit.ArkGraphics2D'; 602 603class DrawingRenderNode extends RenderNode { 604 draw(context : DrawContext) { 605 const canvas = context.canvas; 606 const pen = new drawing.Pen(); 607 pen.setStrokeWidth(5); 608 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 609 let maskFilter = drawing.MaskFilter.createBlurMaskFilter(drawing.BlurType.OUTER, 10); 610 pen.setMaskFilter(maskFilter); 611 } 612} 613``` 614 615## setPathEffect<sup>12+</sup> 616 617setPathEffect(effect: PathEffect): void 618 619设置画笔路径效果。 620 621**系统能力:** SystemCapability.Graphics.Drawing 622 623**参数:** 624 625| 参数名 | 类型 | 必填 | 说明 | 626| ------- | ------------------------- | ---- | ------------ | 627| effect | [PathEffect](arkts-apis-graphics-drawing-PathEffect.md) | 是 | 路径效果对象。null表示清空路径效果。 | 628 629**错误码:** 630 631以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 632 633| 错误码ID | 错误信息 | 634| ------- | --------------------------------------------| 635| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 636 637**示例:** 638 639```ts 640import { RenderNode } from '@kit.ArkUI'; 641import { common2D, drawing } from '@kit.ArkGraphics2D'; 642 643class DrawingRenderNode extends RenderNode { 644 draw(context : DrawContext) { 645 const canvas = context.canvas; 646 const pen = new drawing.Pen(); 647 pen.setStrokeWidth(5); 648 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 649 let pathEffect = drawing.PathEffect.createDashPathEffect([30, 10], 0); 650 pen.setPathEffect(pathEffect); 651 } 652} 653``` 654 655## setShaderEffect<sup>12+</sup> 656 657setShaderEffect(shaderEffect: ShaderEffect): void 658 659设置画笔着色器效果。 660 661**系统能力:** SystemCapability.Graphics.Drawing 662 663**参数:** 664 665| 参数名 | 类型 | 必填 | 说明 | 666| ------- | ------------------------- | ---- | ------------ | 667| shaderEffect | [ShaderEffect](arkts-apis-graphics-drawing-ShaderEffect.md) | 是 | 着色器对象。null表示清空着色器效果。 | 668 669**错误码:** 670 671以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 672 673| 错误码ID | 错误信息 | 674| ------- | --------------------------------------------| 675| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 676 677**示例:** 678 679```ts 680import { drawing } from '@kit.ArkGraphics2D'; 681 682const pen = new drawing.Pen(); 683let shaderEffect = drawing.ShaderEffect.createLinearGradient({x: 100, y: 100}, {x: 300, y: 300}, [0xFF00FF00, 0xFFFF0000], drawing.TileMode.REPEAT); 684pen.setShaderEffect(shaderEffect); 685``` 686 687## setShadowLayer<sup>12+</sup> 688 689setShadowLayer(shadowLayer: ShadowLayer): void 690 691设置画笔阴影层效果。当前仅在绘制文字时生效。 692 693**系统能力:** SystemCapability.Graphics.Drawing 694 695**参数:** 696 697| 参数名 | 类型 | 必填 | 说明 | 698| ------- | ------------------------- | ---- | --------- | 699| shadowLayer | [ShadowLayer](arkts-apis-graphics-drawing-ShadowLayer.md) | 是 | 阴影层对象。null表示清空阴影层效果。 | 700 701**错误码:** 702 703以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 704 705| 错误码ID | 错误信息 | 706| ------- | --------------------------------------------| 707| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 708 709**示例:** 710 711```ts 712import { RenderNode } from '@kit.ArkUI'; 713import { common2D, drawing } from '@kit.ArkGraphics2D'; 714 715class DrawingRenderNode extends RenderNode { 716 draw(context : DrawContext) { 717 const canvas = context.canvas; 718 let font = new drawing.Font(); 719 font.setSize(60); 720 let textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8); 721 let pen = new drawing.Pen(); 722 pen.setStrokeWidth(2.0); 723 let pen_color : common2D.Color = {alpha: 0xFF, red: 0xFF, green: 0x00, blue: 0x00}; 724 pen.setColor(pen_color); 725 canvas.attachPen(pen); 726 canvas.drawTextBlob(textBlob, 100, 100); 727 canvas.detachPen(); 728 let color : common2D.Color = {alpha: 0xFF, red: 0x00, green: 0xFF, blue: 0x00}; 729 let shadowLayer = drawing.ShadowLayer.create(3, -3, 3, color); 730 pen.setShadowLayer(shadowLayer); 731 canvas.attachPen(pen); 732 canvas.drawTextBlob(textBlob, 100, 200); 733 canvas.detachPen(); 734 } 735} 736``` 737 738## setBlendMode 739 740setBlendMode(mode: BlendMode) : void 741 742设置画笔的混合模式。 743 744**系统能力:** SystemCapability.Graphics.Drawing 745 746**参数:** 747 748| 参数名 | 类型 | 必填 | 说明 | 749| ------ | ----------------------- | ---- | ---------------- | 750| mode | [BlendMode](arkts-apis-graphics-drawing-e.md#blendmode) | 是 | 颜色的混合模式。 | 751 752**错误码:** 753 754以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 755 756| 错误码ID | 错误信息 | 757| ------- | --------------------------------------------| 758| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 759 760**示例:** 761 762```ts 763import { drawing } from '@kit.ArkGraphics2D'; 764 765const pen = new drawing.Pen(); 766pen.setBlendMode(drawing.BlendMode.SRC); 767``` 768 769## setJoinStyle<sup>12+</sup> 770 771setJoinStyle(style: JoinStyle): void 772 773设置画笔绘制转角的样式。未调用此接口设置时,系统默认的转角样式为MITER_JOIN。 774 775**系统能力:** SystemCapability.Graphics.Drawing 776 777**参数:** 778 779| 参数名 | 类型 | 必填 | 说明 | 780| ------ | ----------------------- | ---- | --------------- | 781| style | [JoinStyle](arkts-apis-graphics-drawing-e.md#joinstyle12) | 是 | 折线转角样式。 | 782 783**错误码:** 784 785以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 786 787| 错误码ID | 错误信息 | 788| ------- | --------------------------------------------| 789| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 790 791**示例:** 792 793```ts 794import { RenderNode } from '@kit.ArkUI'; 795import { common2D, drawing } from '@kit.ArkGraphics2D'; 796 797class DrawingRenderNode extends RenderNode { 798 draw(context : DrawContext) { 799 const canvas = context.canvas; 800 const pen = new drawing.Pen(); 801 pen.setStrokeWidth(5); 802 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 803 pen.setJoinStyle(drawing.JoinStyle.ROUND_JOIN); 804 } 805} 806``` 807 808## getJoinStyle<sup>12+</sup> 809 810getJoinStyle(): JoinStyle 811 812获取画笔绘制转角的样式。 813 814**系统能力:** SystemCapability.Graphics.Drawing 815 816**返回值:** 817 818| 类型 | 说明 | 819| ------------- | ---------------------- | 820| JoinStyle | 返回折线转角的样式。 | 821 822**示例:** 823 824```ts 825import { RenderNode } from '@kit.ArkUI'; 826import { common2D, drawing } from '@kit.ArkGraphics2D'; 827 828class DrawingRenderNode extends RenderNode { 829 draw(context : DrawContext) { 830 const canvas = context.canvas; 831 const pen = new drawing.Pen(); 832 pen.setStrokeWidth(5); 833 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 834 pen.setJoinStyle(drawing.JoinStyle.ROUND_JOIN); 835 let joinStyle = pen.getJoinStyle(); 836 } 837} 838``` 839 840## setCapStyle<sup>12+</sup> 841 842setCapStyle(style: CapStyle): void 843 844设置画笔的线帽样式。未调用此接口设置时,系统默认的线帽样式为FLAT_CAP。 845 846**系统能力:** SystemCapability.Graphics.Drawing 847 848**参数:** 849 850| 参数名 | 类型 | 必填 | 说明 | 851| ------ | ----------------------- | ---- | --------------------- | 852| style | [CapStyle](arkts-apis-graphics-drawing-e.md#capstyle12) | 是 | 描述画笔的线帽样式。 | 853 854**错误码:** 855 856以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 857 858| 错误码ID | 错误信息 | 859| ------- | --------------------------------------------| 860| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed. | 861 862**示例:** 863 864```ts 865import { RenderNode } from '@kit.ArkUI'; 866import { common2D, drawing } from '@kit.ArkGraphics2D'; 867 868class DrawingRenderNode extends RenderNode { 869 draw(context : DrawContext) { 870 const canvas = context.canvas; 871 const pen = new drawing.Pen(); 872 pen.setStrokeWidth(5); 873 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 874 pen.setCapStyle(drawing.CapStyle.SQUARE_CAP); 875 } 876} 877``` 878 879## getCapStyle<sup>12+</sup> 880 881getCapStyle(): CapStyle 882 883获取画笔的线帽样式。 884 885**系统能力:** SystemCapability.Graphics.Drawing 886 887**返回值:** 888 889| 类型 | 说明 | 890| ------------ | ------------------ | 891| CapStyle | 返回画笔的线帽样式。 | 892 893**示例:** 894 895```ts 896import { RenderNode } from '@kit.ArkUI'; 897import { common2D, drawing } from '@kit.ArkGraphics2D'; 898 899class DrawingRenderNode extends RenderNode { 900 draw(context : DrawContext) { 901 const canvas = context.canvas; 902 const pen = new drawing.Pen(); 903 pen.setStrokeWidth(5); 904 pen.setColor({alpha: 255, red: 255, green: 0, blue: 0}); 905 pen.setCapStyle(drawing.CapStyle.SQUARE_CAP); 906 let capStyle = pen.getCapStyle(); 907 } 908} 909``` 910 911## setDither 912 913setDither(dither: boolean) : void 914 915开启画笔的抖动绘制效果。抖动绘制可以使得绘制出的颜色更加真实。 916 917**系统能力:** SystemCapability.Graphics.Drawing 918 919**参数:** 920 921| 参数名 | 类型 | 必填 | 说明 | 922| ------ | ------- | ---- | --------------------------------------------------------- | 923| dither | boolean | 是 | 是否开启画笔的抖动绘制效果。true表示开启,false表示关闭。 | 924 925**错误码:** 926 927以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 928 929| 错误码ID | 错误信息 | 930| ------- | --------------------------------------------| 931| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 932 933**示例:** 934 935```ts 936import { drawing } from '@kit.ArkGraphics2D'; 937 938const pen = new drawing.Pen(); 939pen.setDither(true); 940``` 941 942## getFillPath<sup>12+</sup> 943 944getFillPath(src: Path, dst: Path): boolean 945 946获取使用画笔绘制的源路径轮廓,并用目标路径表示。 947 948**系统能力:** SystemCapability.Graphics.Drawing 949 950**参数:** 951 952| 参数名 | 类型 | 必填 | 说明 | 953| -------- | -------------------------------------------- | ---- | ------------------------------- | 954| src | [Path](arkts-apis-graphics-drawing-Path.md) | 是 | 源路径对象。 | 955| dst | [Path](arkts-apis-graphics-drawing-Path.md) | 是 | 目标路径对象。 | 956 957**返回值:** 958 959| 类型 | 说明 | 960| --------------------- | -------------- | 961| boolean | 返回获取源路径轮廓是否成功,true表示成功,false表示失败。 | 962 963**错误码:** 964 965以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 966 967| 错误码ID | 错误信息 | 968| ------- | --------------------------------------------| 969| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types. | 970 971**示例:** 972 973```ts 974import { drawing } from '@kit.ArkGraphics2D'; 975 976let pen = new drawing.Pen(); 977let pathSrc: drawing.Path = new drawing.Path(); 978let pathDst: drawing.Path = new drawing.Path(); 979pathSrc.moveTo(0, 0); 980pathSrc.lineTo(700, 700); 981let value = pen.getFillPath(pathSrc, pathDst); 982``` 983 984## reset<sup>12+</sup> 985 986reset(): void 987 988重置当前画笔为初始状态。 989 990**系统能力:** SystemCapability.Graphics.Drawing 991 992**示例:** 993 994```ts 995import { drawing } from '@kit.ArkGraphics2D'; 996 997const pen = new drawing.Pen(); 998pen.reset(); 999```