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