harmony 鸿蒙Polyline
Polyline
折线绘制组件。
说明:
该组件从API version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
该组件从API version 20开始支持使用AttributeUpdater类的updateConstructorParams接口更新构造参数。
子组件
无
接口
Polyline(options?: PolylineOptions)
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | PolylineOptions | 否 | Polyline绘制区域。 |
PolylineOptions18+对象说明
用于描述Polyline绘制区域。
卡片能力: 从API version 18开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 18开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
width | Length | 否 | 宽度,取值范围≥0。 默认值:0 默认单位:vp 异常值按照默认值处理。 |
height | Length | 否 | 高度,取值范围≥0。 默认值:0 默认单位:vp 异常值按照默认值处理。 |
属性
除支持通用属性外,还支持以下属性:
points
points(value: Array<any>)
设置折线经过坐标点列表,支持attributeModifier动态设置属性方法。异常值按照默认值处理。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | Array<Point> | 是 | 折线经过坐标点列表。 默认值:[] 默认单位:vp |
fill
fill(value: ResourceColor)
设置填充区域的颜色,支持attributeModifier动态设置属性方法,异常值按照默认值处理。与通用属性foregroundColor同时设置时,后设置的属性生效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | ResourceColor | 是 | 填充区域颜色。 默认值:Color.Black |
fillOpacity
fillOpacity(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 |
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<any> | 是 | 线条间隙。 默认值:[] 默认单位: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动态设置属性方法。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | LineJoinStyle | 是 | 线条拐角绘制样式。 默认值:LineJoinStyle.Miter |
strokeMiterLimit
strokeMiterLimit(value: number|string)
设置斜接长度与边框宽度比值的极限值,支持attributeModifier动态设置属性方法。斜接长度表示外边框外边交点到内边交点的距离,边框宽度即strokeWidth属性的值。该属性取值仅在strokeLineJoin属性取值LineJoinStyle.Miter时生效。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | number | string | 是 | 斜接长度与边框宽度比值的极限值。 默认值:4 该属性的合法值应当大于等于1.0,当取值范围在[0,1)时按1.0处理,其余异常值按默认值处理。 |
strokeOpacity
strokeOpacity(value: number|string|Resource)
设置线条透明度,支持attributeModifier动态设置属性方法。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
原子化服务API: 从API version 11开始,该接口支持在原子化服务中使用。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
value | number | string | Resource | 是 | 线条透明度,取值范围是[0.0, 1.0]。 默认值:1 若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理。 |
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 |
Point
点坐标类型。
卡片能力: 从API version 9开始,该接口支持在ArkTS卡片中使用。
名称 | 类型定义 | 描述 |
---|---|---|
Point | [number, number] | 第一个参数为x轴坐标,第二个参数为y轴坐标(相对坐标)。 |
示例
示例1(组件属性绘制)
通过points、fillOpacity、stroke、strokeLineJoin、strokeLineCap属性分别绘制折线的经过坐标、透明度、边框颜色、拐角样式、端点样式。
// xxx.ets
@Entry
@Component
struct PolylineExample {
build() {
Column({ space: 10 }) {
// 在 100 * 100 的矩形框中绘制一段折线,起点(0, 0),经过(20,60),到达终点(100, 100)
Polyline({ width: 100, height: 100 })
.points([[0, 0], [20, 60], [100, 100]])
.fillOpacity(0)
.stroke(Color.Blue)
.strokeWidth(3)
// 在 100 * 100 的矩形框中绘制一段折线,起点(20, 0),经过(0,100),到达终点(100, 90)
Polyline()
.width(100)
.height(100)
.fillOpacity(0)
.stroke(Color.Red)
.strokeWidth(8)
.points([[20, 0], [0, 100], [100, 90]])
// 设置折线拐角处为圆弧
.strokeLineJoin(LineJoinStyle.Round)
// 设置折线两端为半圆
.strokeLineCap(LineCapStyle.Round)
}.width('100%')
}
}
示例2(宽和高使用不同参数类型绘制折线)
width、height属性分别使用不同的长度类型绘制图形。
// xxx.ets
@Entry
@Component
struct PolylineTypeExample {
build() {
Column({ space: 10 }) {
// 在 100 * 100 的矩形框中绘制一段折线,起点(0, 0),经过(20,60),到达终点(100, 100)
Polyline({ width: '100', height: '100' })//使用string类型
.points([[0, 0], [20, 60], [100, 100]])
.fillOpacity(0)
.stroke(Color.Blue)
.strokeWidth(3)
Polyline({ width: 100, height: 100 })// 使用number类型
.points([[0, 0], [20, 60], [100, 100]])
.fillOpacity(0)
.stroke(Color.Blue)
.strokeWidth(3)
Polyline({ width: $r('app.string.PolylineWidth'), height: $r('app.string.PolylineHeight') })// 使用Resource类型,需用户自定义
.points([[0, 0], [20, 60], [100, 100]])
.fillOpacity(0)
.stroke(Color.Blue)
.strokeWidth(3)
}.width('100%')
}
}
示例3(使用attributeModifier动态设置Polyline组件的属性)
以下示例展示了如何使用attributeModifier动态设置Polyline组件的points、fill、fillOpacity、stroke、strokeDashArray、strokeDashOffset、strokeLineCap、strokeLineJoin、strokeMiterLimit、strokeOpacity、strokeWidth和antiAlias属性。
// xxx.ets
class MyPolylineModifier implements AttributeModifier<PolylineAttribute> {
applyNormalAttribute(instance: PolylineAttribute): void {
// 折线起点(0, 0),经过(50, 100),到达终点(100, 0),填充颜色#707070,填充透明度0.5,边框颜色#2787D9,边框间隙[20],向左偏移15,线条两端样式为半圆,拐角样式使用尖角连接路径段,斜接长度与边框宽度比值的极限值为5,边框透明度0.5,边框宽度10,抗锯齿开启
instance.points([[0, 0], [50, 100], [100, 0]])
instance.fill("#707070")
instance.fillOpacity(0.5)
instance.stroke("#2787D9")
instance.strokeDashArray([20])
instance.strokeDashOffset("15")
instance.strokeLineCap(LineCapStyle.Round)
instance.strokeLineJoin(LineJoinStyle.Miter)
instance.strokeMiterLimit(5)
instance.strokeOpacity(0.5)
instance.strokeWidth(10)
instance.antiAlias(true)
}
}
@Entry
@Component
struct PolylineModifierDemo {
@State modifier: MyPolylineModifier = new MyPolylineModifier()
build() {
Column() {
Polyline()
.width(100)
.height(100)
.attributeModifier(this.modifier)
.offset({ x: 20, y: 20 })
}
}
}
你可能感兴趣的鸿蒙文章
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦