1# Shape 2 3绘制组件的父组件,父组件中会描述所有绘制组件均支持的通用属性。 4 51、绘制组件使用Shape作为父组件,实现类似SVG的效果。 6 72、绘制组件单独使用,用于在页面上绘制指定的图形。 8 9> **说明:** 10> 11> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 12 13 14## 子组件 15 16包含[Rect](ts-drawing-components-rect.md)、[Path](ts-drawing-components-path.md)、[Circle](ts-drawing-components-circle.md)、[Ellipse](ts-drawing-components-ellipse.md)、[Polyline](ts-drawing-components-polyline.md)、[Polygon](ts-drawing-components-polygon.md)、[Image](ts-basic-components-image.md)、[Text](ts-basic-components-text.md)、[Column](ts-container-column.md)、[Row](ts-container-row.md)子组件。 17 18 19## 接口 20 21Shape(value?: PixelMap) 22 23从API version 9开始,该接口支持在ArkTS卡片中使用,卡片中不支持使用PixelMap对象。 24 25**参数:** 26 27| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | 28| -------- | -------- | -------- | -------- | -------- | 29| value | [PixelMap](../apis/js-apis-image.md#pixelmap7) | 否 | - | 绘制目标,可将图形绘制在指定的PixelMap对象中,若未设置,则在当前绘制目标中进行绘制。 | 30 31 32## 属性 33 34除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 35 36| 名称 | 类型 | 默认值 | 描述 | 37| -------- | -------- | -------- | -------- | 38| viewPort | {<br/>x?: number \| string,<br/>y?: number \| string,<br/>width?: number \| string,<br/>height?: number \| string<br/>} | { x:0, y:0, width:0, height:0 } | 形状的视口。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性若为string类型, 暂不支持百分比。<br/>异常值按照默认值处理。 | 39| fill | [ResourceColor](ts-types.md) | Color.Black | 设置填充区域颜色。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>异常值按照默认值处理。 | 40| fillOpacity | number \| string \| [Resource](ts-types.md#resource类型) | 1 | 设置填充区域透明度。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>异常值按照默认值处理。 | 41| stroke | [ResourceColor](ts-types.md) | - | 设置边框颜色,不设置时,默认没有边框线条。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>异常值不会绘制边框线条。 | 42| strokeDashArray | Array<Length> | [] | 设置边框间隙。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>异常值按照默认值处理。 | 43| strokeDashOffset | number \| string | 0 | 边框绘制起点的偏移量。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:** <br/>异常值按照默认值处理。 | 44| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | 设置边框端点绘制样式。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。| 45| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | 设置边框拐角绘制样式。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。| 46| strokeMiterLimit | number \| string | 4 | 设置斜接长度与边框宽度比值的极限值。斜接长度表示外边框外边交点到内边交点的距离,边框宽度即strokeWidth属性的值。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性取值需在strokeLineJoin属性取值LineJoinStyle.Miter时生效。 该属性的合法值范围应当大于等于1.0,当取值范围在[0,1)时按1.0处理,其余异常值按默认值处理 | 47| strokeOpacity | number \| string \| [Resource](ts-types.md#resource类型) | 1 | 设置边框透明度。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性的取值范围是[0.0, 1.0],若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理 。 | 48| strokeWidth | number \| string | 1 | 设置边框宽度。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>该属性若为string类型, 暂不支持百分比。<br/>异常值按照默认值处理。 | 49| antiAlias | boolean | true | 是否开启抗锯齿效果。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。| 50| mesh<sup>8+</sup> | Array<number>,number,number | [],0,0 | 设置mesh效果。第一个参数为长度(column + 1)* (row + 1)* 2的数组,它记录了扭曲后的位图各个顶点位置,第二个参数为mesh矩阵列数column,第三个参数为mesh矩阵行数row。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。| 51 52## 示例 53 54```ts 55// xxx.ets 56@Entry 57@Component 58struct ShapeExample { 59 build() { 60 Column({ space: 10 }) { 61 Text('basic').fontSize(11).fontColor(0xCCCCCC).width(320) 62 // 在Shape的(-2, -2)点绘制一个 300 * 50 带边框的矩形,颜色0x317AF7,边框颜色黑色,边框宽度4,边框间隙20,向左偏移10,线条两端样式为半圆,拐角样式圆角,抗锯齿(默认开启) 63 // 在Shape的(-2, 58)点绘制一个 300 * 50 带边框的椭圆,颜色0x317AF7,边框颜色黑色,边框宽度4,边框间隙20,向左偏移10,线条两端样式为半圆,拐角样式圆角,抗锯齿(默认开启) 64 // 在Shape的(-2, 118)点绘制一个 300 * 10 直线路径,颜色0x317AF7,边框颜色黑色,宽度4,间隙20,向左偏移10,线条两端样式为半圆,拐角样式圆角,抗锯齿(默认开启) 65 Shape() { 66 Rect().width(300).height(50) 67 Ellipse().width(300).height(50).offset({ x: 0, y: 60 }) 68 Path().width(300).height(10).commands('M0 0 L900 0').offset({ x: 0, y: 120 }) 69 } 70 .width(350) 71 .height(140) 72 .viewPort({ x: -2, y: -2, width: 304, height: 130 }) 73 .fill(0x317AF7) 74 .stroke(Color.Black) 75 .strokeWidth(4) 76 .strokeDashArray([20]) 77 .strokeDashOffset(10) 78 .strokeLineCap(LineCapStyle.Round) 79 .strokeLineJoin(LineJoinStyle.Round) 80 .antiAlias(true) 81 // 分别在Shape的(0, 0)、(-5, -5)点绘制一个 300 * 50 带边框的矩形,可以看出之所以将视口的起始位置坐标设为负值是因为绘制的起点默认为线宽的中点位置,因此要让边框完全显示则需要让视口偏移半个线宽 82 Shape() { 83 Rect().width(300).height(50) 84 } 85 .width(350) 86 .height(80) 87 .viewPort({ x: 0, y: 0, width: 320, height: 70 }) 88 .fill(0x317AF7) 89 .stroke(Color.Black) 90 .strokeWidth(10) 91 92 Shape() { 93 Rect().width(300).height(50) 94 } 95 .width(350) 96 .height(80) 97 .viewPort({ x: -5, y: -5, width: 320, height: 70 }) 98 .fill(0x317AF7) 99 .stroke(Color.Black) 100 .strokeWidth(10) 101 102 Text('path').fontSize(11).fontColor(0xCCCCCC).width(320) 103 // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,线条间隙20 104 Shape() { 105 Path().width(300).height(10).commands('M0 0 L900 0') 106 } 107 .width(350) 108 .height(20) 109 .viewPort({ x: 0, y: -5, width: 300, height: 20 }) 110 .stroke(0xEE8443) 111 .strokeWidth(10) 112 .strokeDashArray([20]) 113 // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,线条间隙20,向左偏移10 114 Shape() { 115 Path().width(300).height(10).commands('M0 0 L900 0') 116 } 117 .width(350) 118 .height(20) 119 .viewPort({ x: 0, y: -5, width: 300, height: 20 }) 120 .stroke(0xEE8443) 121 .strokeWidth(10) 122 .strokeDashArray([20]) 123 .strokeDashOffset(10) 124 // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,透明度0.5 125 Shape() { 126 Path().width(300).height(10).commands('M0 0 L900 0') 127 } 128 .width(350) 129 .height(20) 130 .viewPort({ x: 0, y: -5, width: 300, height: 20 }) 131 .stroke(0xEE8443) 132 .strokeWidth(10) 133 .strokeOpacity(0.5) 134 // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,线条间隙20,线条两端样式为半圆 135 Shape() { 136 Path().width(300).height(10).commands('M0 0 L900 0') 137 } 138 .width(350) 139 .height(20) 140 .viewPort({ x: 0, y: -5, width: 300, height: 20 }) 141 .stroke(0xEE8443) 142 .strokeWidth(10) 143 .strokeDashArray([20]) 144 .strokeLineCap(LineCapStyle.Round) 145 // 在Shape的(-80, -5)点绘制一个封闭路径,颜色0x317AF7,线条宽度10,边框颜色0xEE8443,拐角样式锐角(默认值) 146 Shape() { 147 Path().width(200).height(60).commands('M0 0 L400 0 L400 150 Z') 148 } 149 .width(300) 150 .height(200) 151 .viewPort({ x: -20, y: -5, width: 310, height: 90 }) 152 .fill(0x317AF7) 153 .stroke(0xEE8443) 154 .strokeWidth(10) 155 .strokeLineJoin(LineJoinStyle.Miter) 156 .strokeMiterLimit(5) 157 }.width('100%').margin({ top: 15 }) 158 } 159} 160``` 161 162 163、