ts-drawing-components-shape.md 8.2 KB
Newer Older
Z
zengyawen 已提交
1 2
# Shape

Z
zengyawen 已提交
3 4
绘制组件的父组件,父组件中会描述所有绘制组件均支持的通用属性。

Z
zengyawen 已提交
5

Z
zengyawen 已提交
6 7 8 9
1、绘制组件使用Shape作为父组件,实现类似SVG的效果。

2、绘制组件单独使用,用于在页面上绘制指定的图形。

T
third  
tianyu 已提交
10 11 12
>  **说明:**
>
>  该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
13

Z
zengyawen 已提交
14

Z
zengyawen 已提交
15
## 子组件
Z
zengyawen 已提交
16

T
tianyu 已提交
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)子组件。
Z
zengyawen 已提交
18 19


Z
zengyawen 已提交
20
## 接口
Z
zengyawen 已提交
21

T
third  
tianyu 已提交
22 23
Shape(value?: PixelMap)

L
lanyill 已提交
24 25
从API version 9开始,该接口支持在ArkTS卡片中使用。

Y
yamila 已提交
26 27 28 29 30
**参数:**

| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
| -------- | -------- | -------- | -------- | -------- |
| value | [PixelMap](../apis/js-apis-image.md#pixelmap7) | 否 | - | 绘制目标,可将图形绘制在指定的PixelMap对象中,若未设置,则在当前绘制目标中进行绘制。 |
Z
zengyawen 已提交
31 32


Z
zengyawen 已提交
33
## 属性
Z
zengyawen 已提交
34

L
luoying_ace_admin 已提交
35 36
除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:

Y
yamila 已提交
37 38
| 名称 | 类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
L
lanyill 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
| viewPort | {<br/>x?:&nbsp;number \| string,<br/>y?:&nbsp;number \| string,<br/>width?:&nbsp;number \| string,<br/>height?:&nbsp;number \| string<br/>} | { x:0, y:0, width:0, height:0 } | 形状的视口。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| fill | [ResourceColor](ts-types.md) | Color.Black | 设置填充区域颜色。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| fillOpacity | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource类型) | 1 | 设置填充区域透明度。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| stroke | [ResourceColor](ts-types.md) | - | 设置边框颜色,不设置时,默认没有边框线条。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| strokeDashArray | Array&lt;Length&gt; | [] | 设置边框间隙。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| strokeDashOffset | number&nbsp;\|&nbsp;string | 0 | 边框绘制起点的偏移量。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | 设置边框端点绘制样式。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | 设置边框拐角绘制样式。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| strokeMiterLimit | number&nbsp;\|&nbsp;string | 4 | 设置斜接长度与边框宽度比值的极限值。斜接长度表示外边框外边交点到内边交点的距离,边框宽度即strokeWidth属性的值。<br/>**说明:**<br/>该属性取值需大于等于1,且在strokeLineJoin属性取值LineJoinStyle.Miter时生效。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| strokeOpacity | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[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卡片中使用。|
| strokeWidth | number&nbsp;\|&nbsp;string | 1 | 设置边框宽度。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| antiAlias | boolean | true | 是否开启抗锯齿效果。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
| mesh<sup>8+</sup> | Array&lt;number&gt;,number,number | [],0,0 | 设置mesh效果。第一个参数为长度(column + 1)* (row + 1)* 2的数组,它记录了扭曲后的位图各个顶点位置,第二个参数为mesh矩阵列数column,第三个参数为mesh矩阵行数row。 <br/>从API version 9开始,该接口支持在ArkTS卡片中使用。|
Z
zengyawen 已提交
52

T
tianyu 已提交
53 54 55
## 示例

### 示例1
Z
zengyawen 已提交
56

H
geshi  
HelloCrease 已提交
57 58
```ts
// xxx.ets
Z
zengyawen 已提交
59 60 61 62
@Entry
@Component
struct ShapeExample {
  build() {
63 64 65 66 67
    Column({ space: 10 }) {
      Text('basic').fontSize(11).fontColor(0xCCCCCC).width(320)
      // 在Shape的(-2, -2)点绘制一个 300 * 50 带边框的矩形,颜色0x317AF7,边框颜色黑色,边框宽度4,边框间隙20,向左偏移10,线条两端样式为半圆,拐角样式圆角,抗锯齿(默认开启)
      // 在Shape的(-2, 58)点绘制一个 300 * 50 带边框的椭圆,颜色0x317AF7,边框颜色黑色,边框宽度4,边框间隙20,向左偏移10,线条两端样式为半圆,拐角样式圆角,抗锯齿(默认开启)
      // 在Shape的(-2, 118)点绘制一个 300 * 10 直线路径,颜色0x317AF7,边框颜色黑色,宽度4,间隙20,向左偏移10,线条两端样式为半圆,拐角样式圆角,抗锯齿(默认开启)
Z
zengyawen 已提交
68 69 70 71 72
      Shape() {
        Rect().width(300).height(50)
        Ellipse().width(300).height(50).offset({ x: 0, y: 60 })
        Path().width(300).height(10).commands('M0 0 L900 0').offset({ x: 0, y: 120 })
      }
Z
zengyawen 已提交
73
      .viewPort({ x: -2, y: -2, width: 304, height: 130 })
74 75 76 77 78 79 80 81 82
      .fill(0x317AF7)
      .stroke(Color.Black)
      .strokeWidth(4)
      .strokeDashArray([20])
      .strokeDashOffset(10)
      .strokeLineCap(LineCapStyle.Round)
      .strokeLineJoin(LineJoinStyle.Round)
      .antiAlias(true)
      // 分别在Shape的(0, 0)、(-5, -5)点绘制一个 300 * 50 带边框的矩形,可以看出之所以将视口的起始位置坐标设为负值是因为绘制的起点默认为线宽的中点位置,因此要让边框完全显示则需要让视口偏移半个线宽
Z
zengyawen 已提交
83 84
      Shape() {
        Rect().width(300).height(50)
85 86 87 88 89
      }
      .viewPort({ x: 0, y: 0, width: 320, height: 70 })
      .fill(0x317AF7)
      .stroke(Color.Black)
      .strokeWidth(10)
Z
zengyawen 已提交
90 91

      Shape() {
92 93 94 95 96 97 98 99 100
        Rect().width(300).height(50)
      }
      .viewPort({ x: -5, y: -5, width: 320, height: 70 })
      .fill(0x317AF7)
      .stroke(Color.Black)
      .strokeWidth(10)

      Text('path').fontSize(11).fontColor(0xCCCCCC).width(320)
      // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,线条间隙20
Z
zengyawen 已提交
101 102 103
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
104
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
105 106 107 108
      .stroke(0xEE8443)
      .strokeWidth(10)
      .strokeDashArray([20])
      // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,线条间隙20,向左偏移10
Z
zengyawen 已提交
109 110
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
111 112 113 114 115 116 117
      }
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
      .stroke(0xEE8443)
      .strokeWidth(10)
      .strokeDashArray([20])
      .strokeDashOffset(10)
      // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,透明度0.5
Z
zengyawen 已提交
118 119 120
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
121
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
122 123 124 125
      .stroke(0xEE8443)
      .strokeWidth(10)
      .strokeOpacity(0.5)
      // 在Shape的(0, -5)点绘制一条直线路径,颜色0xEE8443,线条宽度10,线条间隙20,线条两端样式为半圆
Z
zengyawen 已提交
126
      Shape() {
127
        Path().width(300).height(10).commands('M0 0 L900 0')
Z
zengyawen 已提交
128
      }
129 130 131 132 133 134
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
      .stroke(0xEE8443)
      .strokeWidth(10)
      .strokeDashArray([20])
      .strokeLineCap(LineCapStyle.Round)
      // 在Shape的(-80, -5)点绘制一个封闭路径,颜色0x317AF7,线条宽度10,边框颜色0xEE8443,拐角样式锐角(默认值)
Z
zengyawen 已提交
135
      Shape() {
136
        Path().width(200).height(60).commands('M0 0 L400 0 L400 150 Z')
Z
zengyawen 已提交
137
      }
138 139 140 141 142 143
      .viewPort({ x: -80, y: -5, width: 310, height: 90 })
      .fill(0x317AF7)
      .stroke(0xEE8443)
      .strokeWidth(10)
      .strokeLineJoin(LineJoinStyle.Miter)
      .strokeMiterLimit(5)
Z
zengyawen 已提交
144 145 146 147 148
    }.width('100%').margin({ top: 15 })
  }
}
```

T
tianyu 已提交
149
![zh-cn_image_0000001184628104](figures/zh-cn_image_0000001184628104.png)