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