• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Line
2
3直线绘制组件。
4
5>  **说明:**
6>
7> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8
9## 子组件
10
1112
13
14## 接口
15
16Line(value?: {width?: string | number, height?: string | number})
17
18从API version 9开始,该接口支持在ArkTS卡片中使用。
19
20**参数:**
21
22| 参数名 | 参数类型 | 必填 | 默认值 | 参数描述 |
23| -------- | -------- | -------- | -------- | -------- |
24| width | string \| number | 否 | 0 | 宽度。<br/>**说明:**<br/>异常值按照默认值处理。 |
25| height | string \| number | 否 | 0 | 高度。<br/>**说明:**<br/>异常值按照默认值处理。 |
26
27
28
29## 属性
30
31除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性:
32
33| 名称 | 类型 | 默认值 | 描述 |
34| -------- | -------- | -------- | -------- |
35| startPoint | Array&lt;Length&gt; | [0,&nbsp;0] | 直线起点坐标点(相对坐标),单位vp。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>异常值按照默认值处理。 |
36| endPoint   | Array&lt;Length&gt; | [0,&nbsp;0] | 直线终点坐标点(相对坐标),单位vp。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>异常值按照默认值处理。 |
37| fill | [ResourceColor](ts-types.md#resourcecolor) | Color.Black | 设置填充区域颜色。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>Line组件无法形成闭合区域,该属性设置无效。 |
38| fillOpacity | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[Resource](ts-types.md#resource类型) | 1 | 设置填充区域透明度。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>Line组件无法形成闭合区域,该属性设置无效。 |
39| stroke | [ResourceColor](ts-types.md) | - | 设置边框颜色,不设置时,默认没有边框线条。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>异常值不会绘制边框线条。 |
40| strokeDashArray | Array&lt;Length&gt; | [] | 设置线条间隙。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>异常值按照默认值处理。 |
41| strokeDashOffset | number&nbsp;\|&nbsp;string | 0 | 线条绘制起点的偏移量。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>异常值按照默认值处理。 |
42| strokeLineCap | [LineCapStyle](ts-appendix-enums.md#linecapstyle) | LineCapStyle.Butt | 设置线条端点绘制样式。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 |
43| strokeLineJoin | [LineJoinStyle](ts-appendix-enums.md#linejoinstyle) | LineJoinStyle.Miter | 设置线条拐角绘制样式。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>Line组件无法形成拐角,该属性设置无效。 |
44| strokeMiterLimit | number&nbsp;\|&nbsp;string | 4 | 设置锐角绘制成斜角的极限值。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**说明:**<br/>Line组件无法设置锐角图形,该属性设置无效。 |
45| strokeOpacity | number&nbsp;\|&nbsp;string&nbsp;\|&nbsp;[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处理 。 |
46| strokeWidth | Length | 1 | 设置线条宽度。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 <br/>**说明:**<br/>该属性若为string类型, 暂不支持百分比。<br/>异常值按照默认值处理。 |
47| antiAlias | boolean | true | 是否开启抗锯齿效果。<br/>从API version 9开始,该接口支持在ArkTS卡片中使用。 |
48
49
50
51## 示例
52
53### 示例1
54
55```ts
56// xxx.ets
57@Entry
58@Component
59struct LineExample {
60  build() {
61    Column({ space: 10 }) {
62      // 线条绘制的起止点坐标均是相对于Line组件本身绘制区域的坐标
63      Line()
64        .width(200)
65        .height(150)
66        .startPoint([0, 0])
67        .endPoint([50, 100])
68        .stroke(Color.Black)
69        .backgroundColor('#F5F5F5')
70      Line()
71        .width(200)
72        .height(150)
73        .startPoint([50, 50])
74        .endPoint([150, 150])
75        .strokeWidth(5)
76        .stroke(Color.Orange)
77        .strokeOpacity(0.5)
78        .backgroundColor('#F5F5F5')
79      // strokeDashOffset用于定义关联虚线strokeDashArray数组渲染时的偏移
80      Line()
81        .width(200)
82        .height(150)
83        .startPoint([0, 0])
84        .endPoint([100, 100])
85        .stroke(Color.Black)
86        .strokeWidth(3)
87        .strokeDashArray([10, 3])
88        .strokeDashOffset(5)
89        .backgroundColor('#F5F5F5')
90      // 当坐标点设置的值超出Line组件的宽高范围时,线条会画出组件绘制区域
91      Line()
92        .width(50)
93        .height(50)
94        .startPoint([0, 0])
95        .endPoint([100, 100])
96        .stroke(Color.Black)
97        .strokeWidth(3)
98        .strokeDashArray([10, 3])
99        .backgroundColor('#F5F5F5')
100    }
101  }
102}
103```
104
105![zh-cn_image_0000001219982725](figures/zh-cn_image_0000001219982725.png)
106
107### 示例2
108
109```ts
110// xxx.ets
111@Entry
112@Component
113struct LineExample1 {
114  build() {
115    Row({ space: 10 }) {
116      // 当LineCapStyle值为Butt时
117      Line()
118        .width(100)
119        .height(200)
120        .startPoint([50, 50])
121        .endPoint([50, 200])
122        .stroke(Color.Black)
123        .strokeWidth(20)
124        .strokeLineCap(LineCapStyle.Butt)
125        .backgroundColor('#F5F5F5').margin(10)
126      // 当LineCapStyle值为Round时
127      Line()
128        .width(100)
129        .height(200)
130        .startPoint([50, 50])
131        .endPoint([50, 200])
132        .stroke(Color.Black)
133        .strokeWidth(20)
134        .strokeLineCap(LineCapStyle.Round)
135        .backgroundColor('#F5F5F5')
136      // 当LineCapStyle值为Square时
137      Line()
138        .width(100)
139        .height(200)
140        .startPoint([50, 50])
141        .endPoint([50, 200])
142        .stroke(Color.Black)
143        .strokeWidth(20)
144        .strokeLineCap(LineCapStyle.Square)
145        .backgroundColor('#F5F5F5')
146    }
147  }
148}
149```
150
151![zh-cn_image1_0000001219982725](figures/zh-cn_image1_0000001219982725.png)
152
153### 示例3
154
155```ts
156// xxx.ets
157@Entry
158@Component
159struct LineExample {
160  build() {
161    Column() {
162      Line()
163        .width(300)
164        .height(30)
165        .startPoint([50, 30])
166        .endPoint([300, 30])
167        .stroke(Color.Black)
168        .strokeWidth(10)
169      // 设置strokeDashArray的数组间隔为 50
170      Line()
171        .width(300)
172        .height(30)
173        .startPoint([50, 20])
174        .endPoint([300, 20])
175        .stroke(Color.Black)
176        .strokeWidth(10)
177        .strokeDashArray([50])
178      // 设置strokeDashArray的数组间隔为 50, 10
179      Line()
180        .width(300)
181        .height(30)
182        .startPoint([50, 20])
183        .endPoint([300, 20])
184        .stroke(Color.Black)
185        .strokeWidth(10)
186        .strokeDashArray([50, 10])
187      // 设置strokeDashArray的数组间隔为 50, 10, 20
188      Line()
189        .width(300)
190        .height(30)
191        .startPoint([50, 20])
192        .endPoint([300, 20])
193        .stroke(Color.Black)
194        .strokeWidth(10)
195        .strokeDashArray([50, 10, 20])
196      // 设置strokeDashArray的数组间隔为 50, 10, 20, 30
197      Line()
198        .width(300)
199        .height(30)
200        .startPoint([50, 20])
201        .endPoint([300, 20])
202        .stroke(Color.Black)
203        .strokeWidth(10)
204        .strokeDashArray([50, 10, 20, 30])
205
206    }
207  }
208}
209```
210
211![zh-cn_image2_0000001219982725](figures/zh-cn_image2_0000001219982725.PNG)
212