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

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5

Z
zengyawen 已提交
6 7 8

绘制组件的父组件,父组件中会描述所有绘制组件均支持的通用属性。

Z
zengyawen 已提交
9

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

Z
zengyawen 已提交
12

Z
zengyawen 已提交
13 14
2、绘制组件单独使用,用于在页面上绘制指定的图形。

Z
zengyawen 已提交
15 16

## 权限列表
Z
zengyawen 已提交
17 18 19



Z
zengyawen 已提交
20

Z
zengyawen 已提交
21
## 子组件
Z
zengyawen 已提交
22

Z
zengyawen 已提交
23
可以包含子组件。
Z
zengyawen 已提交
24 25


Z
zengyawen 已提交
26
## 接口
Z
zengyawen 已提交
27

Z
zengyawen 已提交
28
Shape(value:{target?: PixelMap})
Z
zengyawen 已提交
29

Z
zengyawen 已提交
30 31 32 33
- 参数
  | 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 | 
  | -------- | -------- | -------- | -------- | -------- |
  | target | PixelMap | 否 | null | 绘制目标,可将图形绘制在指定的PixelMap对象中,若未设置,则在当前绘制目标中进行绘制。 | 
Z
zengyawen 已提交
34 35


Z
zengyawen 已提交
36
## 属性
Z
zengyawen 已提交
37

Z
zengyawen 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
| 参数名称 | 参数类型 | 默认值 | 必填 | 参数描述 |
| -------- | -------- | -------- | -------- | -------- |
| viewPort | {<br/>x:&nbsp;Length,<br/>y:&nbsp;Length,<br/>width:&nbsp;Length,<br/>height:&nbsp;Length<br/>} | - | 是 | 形状的视口。 |
| fill | Color | Black | 否 | 填充颜色。 |
| stroke | Color | - | 否 | 边框颜色。 |
| strokeDashArray | Array&lt;Length&gt; | [] | 否 | 设置边框的间隙。 |
| strokeDashOffset | Length | 0 | 否 | 边框绘制起点的偏移量。 |
| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#LineCapStyle枚举说明) | LineCapStyle.Butt | 否 | 路径端点绘制样式。 |
| strokeLineJoin | LineJoinStyle | LineJoinStyle.Miter | 否 | 边框拐角绘制样式。 |
| strokeMiterLimit | number | 4 | 否 | 锐角绘制成斜角的极限值。 |
| strokeOpacity | number | 1 | 否 | 设置边框的不透明度。 |
| strokeWidth | Length | 1 | 否 | 设置边框的宽度。 |
| antiAlias | boolean | true | 否 | 是否开启抗锯齿。 |
Z
zengyawen 已提交
51

Z
zengyawen 已提交
52 53 54 55 56 57
- LineJoinStyle枚举说明
  | 名称 | 描述 | 
  | -------- | -------- |
  | Bevel | 使用斜角连接路径段。 | 
  | Miter | 使用尖角连接路径段。 | 
  | Round | 使用圆角连接路径段。 | 
Z
zengyawen 已提交
58 59


Z
zengyawen 已提交
60
## 示例
Z
zengyawen 已提交
61 62 63 64 65 66 67

```
@Entry
@Component
struct ShapeExample {
  build() {
    Column({ space: 5 }) {
Z
zengyawen 已提交
68
      Text('basic').fontSize(30).fontColor(0xCCCCCC).width(320)
Z
zengyawen 已提交
69 70 71 72 73 74 75 76
      // 在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,尖端样式圆角,拐角样式圆角,抗锯齿(默认开启)
      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 已提交
77
      .viewPort({ x: -2, y: -2, width: 304, height: 130 })
Z
zengyawen 已提交
78 79 80 81 82 83 84 85
      .fill(0x317Af7).stroke(Color.Black).strokeWidth(4)
      .strokeDashArray([20]).strokeDashOffset(10).strokeLineCap(LineCapStyle.Round)
      .strokeLineJoin(LineJoinStyle.Round).antiAlias(true)
      // 在Shape的(-1, -1)点绘制一个 300 * 50 带边框的矩形,颜色0x317Af7,边框颜色黑色,边框宽度2
      Shape() {
        Rect().width(300).height(50)
      }.viewPort({ x: -1, y: -1, width: 302, height: 52 }).fill(0x317Af7).stroke(Color.Black).strokeWidth(2)

Z
zengyawen 已提交
86
      Text('border').fontSize(30).fontColor(0xCCCCCC).width(320).margin({top:30})
Z
zengyawen 已提交
87 88 89
      // 在Shape的(0, -5)点绘制一个 300 * 10 直线,颜色0xEE8443,边框宽度10,边框间隙20
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
Z
zengyawen 已提交
90
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeDashArray([20])
Z
zengyawen 已提交
91 92 93 94
      // 在Shape的(0, -5)点绘制一个 300 * 10 直线,颜色0xEE8443,边框宽度10,边框间隙20,向左偏移10
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
95
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
96 97 98 99
      .stroke(0xEE8443).strokeWidth(10).strokeDashArray([20]).strokeDashOffset(10)
      // 在Shape的(0, -5)点绘制一个 300 * 10 直线,颜色0xEE8443,边框宽度10,透明度0.5
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
Z
zengyawen 已提交
100
      }.viewPort({ x: 0, y: -5, width: 300, height: 20 }).stroke(0xEE8443).strokeWidth(10).strokeOpacity(0.5)
Z
zengyawen 已提交
101 102 103 104
      // 在Shape的(0, -5)点绘制一个 300 * 10 直线,颜色0xEE8443,边框宽度10,边框间隙20,向左偏移10,尖端样式圆角
      Shape() {
        Path().width(300).height(10).commands('M0 0 L900 0')
      }
Z
zengyawen 已提交
105
      .viewPort({ x: 0, y: -5, width: 300, height: 20 })
Z
zengyawen 已提交
106 107 108
      .stroke(0xEE8443).strokeWidth(10).strokeDashArray([20]).strokeLineCap(LineCapStyle.Round)
      // 在Shape的(-5, -5)点绘制一个 300 * 50 带边框的矩形,颜色0x317Af7,边框宽度10,边框颜色0xEE8443,拐角样式圆角
      Shape() {
Z
zengyawen 已提交
109
        Rect().width(300).height(100)
Z
zengyawen 已提交
110
      }
Z
zengyawen 已提交
111
      .viewPort({ x: -5, y: -5, width: 310, height: 120 })
Z
zengyawen 已提交
112 113 114 115 116 117 118 119 120 121 122 123
      .fill(0x317Af7).stroke(0xEE8443).strokeWidth(10).strokeLineJoin(LineJoinStyle.Round)
      Shape() {
        Path().width(300).height(60).commands('M0 0 L400 0 L400 200 Z')
      }
      .viewPort({ x: -80, y: -5, width: 310, height: 100 })
      .fill(0x317Af7).stroke(0xEE8443).strokeWidth(10)
      .strokeLineJoin(LineJoinStyle.Miter).strokeMiterLimit(5)
    }.width('100%').margin({ top: 15 })
  }
}
```

Z
zengyawen 已提交
124
![zh-cn_image_0000001184628104](figures/zh-cn_image_0000001184628104.png)