ts-drawing-components-line.md 5.6 KB
Newer Older
Z
zengyawen 已提交
1 2
# Line

H
geshi  
HelloCrease 已提交
3
>  **说明:**
4
>
Z
zengyawen 已提交
5
> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8 9

直线绘制组件。

Z
zengyawen 已提交
10 11

## 权限列表
Z
zengyawen 已提交
12 13 14



Z
zengyawen 已提交
15 16

## 子组件
Z
zengyawen 已提交
17 18 19



Z
zengyawen 已提交
20 21 22

## 接口

L
fix doc  
luoying_ace_admin 已提交
23
Line(value?: {width?: string | number, height?: string | number})
Z
zengyawen 已提交
24

25 26
**参数:**

T
tianyu 已提交
27
| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
28
| -------- | -------- | -------- | -------- | -------- |
T
tianyu 已提交
29 30
| width | string \| number | 否 | 0 | 宽度。 |
| height | string \| number | 否 | 0 | 高度。 |
Z
zengyawen 已提交
31 32 33 34


## 属性

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

Y
yamila 已提交
37 38 39 40 41 42 43 44 45 46 47 48
| 名称 | 类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
| startPoint | Array<Length> | [0, 0] | 直线起点坐标点(相对坐标),单位vp。 |
| endPoint   | Array<Length> | [0, 0] | 直线终点坐标点(相对坐标),单位vp。 |
| fill | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | 设置填充区域颜色。<br/>**说明:**<br/>Line组件无法形成闭合区域,该属性设置无效。 |
| fillOpacity | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource类型) | 1 | 设置填充区域透明度。<br/>**说明:**<br/>Line组件无法形成闭合区域,该属性设置无效。 |
| stroke | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | 设置线条颜色。 |
| strokeDashArray | Array&lt;Length&gt; | [] | 设置线条间隙。 |
| strokeDashOffset | number&nbsp;\|&nbsp;string | 0 | 线条绘制起点的偏移量。 |
| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | 设置线条端点绘制样式。 |
| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | 设置线条拐角绘制样式。 |
| strokeMiterLimit | number&nbsp;\|&nbsp;string | 4 | 设置锐角绘制成斜角的极限值。<br/>**说明:**<br/>Line组件无法设置锐角图形,该属性设置无效。 |
Y
yamila 已提交
49
| 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。 |
Y
yamila 已提交
50 51
| strokeWidth | Length | 1 | 设置线条宽度。 |
| antiAlias | boolean | true | 是否开启抗锯齿效果。 |
Z
zengyawen 已提交
52 53

## 示例
Z
zengyawen 已提交
54

T
tianyu 已提交
55 56
### 示例1

H
geshi  
HelloCrease 已提交
57 58
```ts
// xxx.ets
Z
zengyawen 已提交
59 60 61 62
@Entry
@Component
struct LineExample {
  build() {
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    Column({ space: 10 }) {
      // 线条绘制的起止点坐标均是相对于Line组件本身绘制区域的坐标
      Line()
        .startPoint([0, 0])
        .endPoint([50, 100])
        .backgroundColor('#F5F5F5')
      Line()
        .width(200)
        .height(200)
        .startPoint([50, 50])
        .endPoint([150, 150])
        .strokeWidth(5)
        .stroke(Color.Orange)
        .strokeOpacity(0.5)
        .backgroundColor('#F5F5F5')
      // 当坐标点设置的值超出Line组件的宽高范围时,线条会画出组件绘制区域
      Line({ width: 50, height: 50 })
        .startPoint([0, 0])
        .endPoint([100, 100])
        .strokeWidth(3)
T
tianyu 已提交
83 84 85 86 87 88 89 90 91
        .strokeDashArray([10, 3])
        .backgroundColor('#F5F5F5')
      // strokeDashOffset用于定义关联虚线strokeDashArray数组渲染时的偏移
      Line({ width: 50, height: 50 })
        .startPoint([0, 0])
        .endPoint([100, 100])
        .strokeWidth(3)
        .strokeDashArray([10, 3])
        .strokeDashOffset(5)
92 93
        .backgroundColor('#F5F5F5')
    }
Z
zengyawen 已提交
94 95 96 97
  }
}
```

T
tianyu 已提交
98
![zh-cn_image_0000001219982725](figures/zh-cn_image_0000001219982725.png)
T
tianyu 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

### 示例2

```ts
// xxx.ets
@Entry
@Component
struct LineExample1 {
  build() {
    Row({ space: 10 }) {
      // 当LineCapStyle值为Butt时
      Line()
        .width(100)
        .height(200)
        .startPoint([50, 50])
        .endPoint([50, 200])
        .strokeWidth(20)
        .strokeLineCap(LineCapStyle.Butt)
        .backgroundColor('#F5F5F5').margin(10)
      // 当LineCapStyle值为Round时
      Line()
        .width(100)
        .height(200)
        .startPoint([50, 50])
        .endPoint([50, 200])
        .strokeWidth(20)
        .strokeLineCap(LineCapStyle.Round)
        .backgroundColor('#F5F5F5')
      // 当LineCapStyle值为Square时
      Line()
        .width(100)
        .height(200)
        .startPoint([50, 50])
        .endPoint([50, 200])
        .strokeWidth(20)
        .strokeLineCap(LineCapStyle.Square)
        .backgroundColor('#F5F5F5')
    }
  }
}
```

Y
yamila 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
![zh-cn_image1_0000001219982725](figures/zh-cn_image1_0000001219982725.png)

### 示例3

```ts
// xxx.ets
@Entry
@Component
struct LineExample {
  build() {
    Column() {
      Line()
        .startPoint([50, 30])
        .endPoint([300, 30])
        .strokeWidth(10)
      // 设置strokeDashArray的数组间隔为 50
      Line()
        .startPoint([50, 20])
        .endPoint([300, 20])
        .strokeWidth(10)
        .strokeDashArray([50])
      // 设置strokeDashArray的数组间隔为 50, 10
      Line()
        .startPoint([50, 20])
        .endPoint([300, 20])
        .strokeWidth(10)
        .strokeDashArray([50, 10])
      // 设置strokeDashArray的数组间隔为 50, 10, 20
      Line()
        .startPoint([50, 20])
        .endPoint([300, 20])
        .strokeWidth(10)
        .strokeDashArray([50, 10, 20])
      // 设置strokeDashArray的数组间隔为 50, 10, 20, 30
      Line()
        .startPoint([50, 20])
        .endPoint([300, 20])
        .strokeWidth(10)
        .strokeDashArray([50, 10, 20, 30])

    }
  }
}
```

![zh-cn_image2_0000001219982725](figures/zh-cn_image2_0000001219982725.PNG)