harmony 鸿蒙Line
Line
直线绘制组件。
说明:
该组件从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
该组件从API version 20开始支持使用AttributeUpdater类的updateConstructorParams接口更新构造参数。
子组件
无
接口
Line(options?: LineOptions)
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | LineOptions | 否 | Line绘制区域。 |
LineOptions18+对象说明
用于描述Line绘制区域。
卡片能力: 从API version 18开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 18开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
width | Length | 否 | 宽度。 值为异常值或缺省时按照自身内容需要的宽度处理。 默认单位:vp |
height | Length | 否 | 高度。 值为异常值或缺省时按照自身内容需要的宽度处理。 默认单位:vp |
属性
除支持通用属性外,还支持以下属性:
startPoint
startPoint(value: Array<any>)
设置直线起点坐标点(相对坐标),支持attributeModifier动态设置属性方法,异常值按照默认值处理。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | Array<Length> | 是 | 直线起点坐标点(相对坐标),单位vp。 默认值:[0, 0] |
endPoint
endPoint(value: Array<any>)
设置直线终点坐标点(相对坐标),支持attributeModifier动态设置属性方法,异常值按照默认值处理。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | Array<Length> | 是 | 直线终点坐标点(相对坐标),单位vp。 默认值:[0, 0] |
fill
fill(value: ResourceColor)
设置填充区域颜色,支持attributeModifier动态设置属性方法。Line组件无法形成闭合区域,该属性设置无效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | ResourceColor | 是 | 填充区域颜色。 默认值:Color.Black |
fillOpacity
fillOpacity(value: number|string|Resource)
设置填充区域透明度,支持attributeModifier动态设置属性方法。Line组件无法形成闭合区域,该属性设置无效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | number | string | Resource | 是 | 填充区域透明度。 默认值:1 |
stroke
stroke(value: ResourceColor)
设置边框颜色,支持attributeModifier动态设置属性方法,不设置时,默认没有边框。异常值不会绘制边框线条。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | ResourceColor | 是 | 边框颜色。 |
strokeDashArray
strokeDashArray(value: Array<any>)
设置边框间隙,支持attributeModifier动态设置属性方法。线段相交时可能会出现重叠现象。取值范围≥0,异常值按照默认值处理。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | Array<Length> | 是 | 边框间隙。 默认值:[] 默认单位:vp |
strokeDashOffset
strokeDashOffset(value: number | string)
设置边框绘制起点的偏移量,支持attributeModifier动态设置属性方法。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | number | string | 是 | 边框绘制起点的偏移量。 默认值:0 默认单位:vp |
strokeLineCap
strokeLineCap(value: LineCapStyle)
设置边框端点绘制样式,支持attributeModifier动态设置属性方法。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | LineCapStyle | 是 | 边框端点绘制样式。 默认值:LineCapStyle.Butt |
strokeLineJoin
strokeLineJoin(value: LineJoinStyle)
设置边框拐角绘制样式,支持attributeModifier动态设置属性方法。Line组件不支持拐角,该属性设置无效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | LineJoinStyle | 是 | 边框拐角绘制样式。 默认值:LineJoinStyle.Miter |
strokeMiterLimit
strokeMiterLimit(value: number|string)
设置锐角绘制成斜角的极限值,支持attributeModifier动态设置属性方法。Line组件不支持设置锐角图形,该属性设置无效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | number | string | 是 | 锐角绘制成斜角的极限值。 默认值:4 |
strokeOpacity
strokeOpacity(value: number|string|Resource)
设置边框透明度,支持attributeModifier动态设置属性方法。该属性的取值范围是[0.0, 1.0],若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | number | string | Resource | 是 | 边框透明度。 默认值:1 |
strokeWidth
strokeWidth(value: Length)
设置边框宽度,支持attributeModifier动态设置属性方法。该属性若为string类型, 暂不支持百分比,百分比按照1px处理。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | Length | 是 | 边框宽度,取值范围≥0。 默认值:1 默认单位:vp 异常值按照默认值处理。 |
antiAlias
antiAlias(value: boolean)
设置是否开启抗锯齿效果,支持attributeModifier动态设置属性方法。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | boolean | 是 | 是否开启抗锯齿效果。 true:开启抗锯齿;false:关闭抗锯齿。 默认值:true |
示例
示例1(组件属性绘制)
通过startPoint、endPoint、fillOpacity、stroke、strokeDashArray、strokeDashOffset属性分别绘制直线的起始点、结束点、透明度、直线颜色、边框间隙、绘制起点。
// xxx.ets
@Entry
@Component
struct LineExample {
build() {
Column({ space: 10 }) {
// 线条绘制的起止点坐标均是相对于Line组件本身绘制区域的坐标
Line()
.width(200)
.height(150)
.startPoint([0, 0])
.endPoint([50, 100])
.stroke(Color.Black)
.backgroundColor('#F5F5F5')
Line()
.width(200)
.height(150)
.startPoint([50, 50])
.endPoint([150, 150])
.strokeWidth(5)
.stroke(Color.Orange)
.strokeOpacity(0.5)
.backgroundColor('#F5F5F5')
// strokeDashOffset用于定义关联虚线strokeDashArray数组渲染时的偏移
Line()
.width(200)
.height(150)
.startPoint([0, 0])
.endPoint([100, 100])
.stroke(Color.Black)
.strokeWidth(3)
.strokeDashArray([10, 3])
.strokeDashOffset(5)
.backgroundColor('#F5F5F5')
// 当坐标点设置的值超出Line组件的宽高范围时,线条会画出组件绘制区域
Line()
.width(50)
.height(50)
.startPoint([0, 0])
.endPoint([100, 100])
.stroke(Color.Black)
.strokeWidth(3)
.strokeDashArray([10, 3])
.backgroundColor('#F5F5F5')
}
}
}
示例2(边框端点绘制)
通过strokeLineCap属性绘制直线的边框端点样式。
// xxx.ets
@Entry
@Component
struct LineExample1 {
build() {
Row({ space: 10 }) {
// 当LineCapStyle值为Butt时
Line()
.width(100)
.height(200)
.startPoint([50, 50])
.endPoint([50, 200])
.stroke(Color.Black)
.strokeWidth(20)
.strokeLineCap(LineCapStyle.Butt)
.backgroundColor('#F5F5F5')
.margin(10)
// 当LineCapStyle值为Round时
Line()
.width(100)
.height(200)
.startPoint([50, 50])
.endPoint([50, 200])
.stroke(Color.Black)
.strokeWidth(20)
.strokeLineCap(LineCapStyle.Round)
.backgroundColor('#F5F5F5')
// 当LineCapStyle值为Square时
Line()
.width(100)
.height(200)
.startPoint([50, 50])
.endPoint([50, 200])
.stroke(Color.Black)
.strokeWidth(20)
.strokeLineCap(LineCapStyle.Square)
.backgroundColor('#F5F5F5')
}
}
}
示例3(边框间隙绘制)
通过strokeDashArray属性绘制直线的边框间隙。
// xxx.ets
@Entry
@Component
struct LineExample {
build() {
Column() {
Line()
.width(300)
.height(30)
.startPoint([50, 30])
.endPoint([300, 30])
.stroke(Color.Black)
.strokeWidth(10)
// 设置strokeDashArray的数组间隔为 50
Line()
.width(300)
.height(30)
.startPoint([50, 20])
.endPoint([300, 20])
.stroke(Color.Black)
.strokeWidth(10)
.strokeDashArray([50])
// 设置strokeDashArray的数组间隔为 50, 10
Line()
.width(300)
.height(30)
.startPoint([50, 20])
.endPoint([300, 20])
.stroke(Color.Black)
.strokeWidth(10)
.strokeDashArray([50, 10])
// 设置strokeDashArray的数组间隔为 50, 10, 20
Line()
.width(300)
.height(30)
.startPoint([50, 20])
.endPoint([300, 20])
.stroke(Color.Black)
.strokeWidth(10)
.strokeDashArray([50, 10, 20])
// 设置strokeDashArray的数组间隔为 50, 10, 20, 30
Line()
.width(300)
.height(30)
.startPoint([50, 20])
.endPoint([300, 20])
.stroke(Color.Black)
.strokeWidth(10)
.strokeDashArray([50, 10, 20, 30])
}
}
}
示例4(宽和高使用不同参数类型绘制直线)
width、height属性分别使用不同的长度类型绘制直线。
// xxx.ets
@Entry
@Component
struct LineTypeExample {
build() {
Column({ space: 10 }) {
// 在200 * 200的区域内绘制一个起始点为(0,0),终点为(150,150),边框宽度为10的直线
Line({ width: '200', height: '200' })// 使用string类型
.startPoint([0, 0])
.endPoint([150, 150])
.stroke(Color.Black)
.strokeWidth(10)
.backgroundColor('#F5F5F5')
.margin(10)
// 在200 * 200的区域内绘制一个起始点为(0,50),终点为(150,150),边框宽度为10的直线
Line({ width: 200, height: 200 })// 使用number类型
.startPoint([0, 50])
.endPoint([150, 150])
.stroke(Color.Black)
.strokeWidth(10)
.backgroundColor('#F5F5F5')
.margin(10)
// 在200 * 200的区域内绘制一个起始点为(0,100),终点为(150,150),边框宽度为10的直线
Line({ width: $r('app.string.LineWidth'), height: $r('app.string.LineHeight') })// 使用Resource类型,需用户自定义
.startPoint([0, 100])
.endPoint([150, 150])
.stroke(Color.Black)
.strokeWidth(10)
.backgroundColor('#F5F5F5')
.margin(10)
}.width('100%')
}
}
示例5(使用attributeModifier动态设置Line组件的属性)
以下示例展示了如何使用attributeModifier动态设置Line组件的startPoint、endPoint、stroke、strokeDashArray、strokeDashOffset、strokeLineCap、strokeOpacity、strokeWidth和antiAlias属性。
// xxx.ets
class MyLineModifier implements AttributeModifier<LineAttribute> {
applyNormalAttribute(instance: LineAttribute): void {
// 一个起始点为(10, 10),终点为(120, 10)的直线,边框颜色#2787D9,边框间隙[20],向左偏移15,线条两端样式为半圆,边框透明度0.5,边框宽度10,抗锯齿开启
instance.startPoint([10,10])
instance.endPoint([120, 10])
instance.stroke("#2787D9")
instance.strokeDashArray([20])
instance.strokeDashOffset("15")
instance.strokeLineCap(LineCapStyle.Round)
instance.strokeOpacity(0.5)
instance.strokeWidth(10)
instance.antiAlias(true)
}
}
@Entry
@Component
struct LineModifierDemo {
@State modifier: MyLineModifier = new MyLineModifier()
build() {
Column() {
Line()
.attributeModifier(this.modifier)
.offset({ x: 20, y: 20 })
}
}
}
你可能感兴趣的鸿蒙文章
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦