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