鸿蒙Harmony-多边形绘制组件Polygon使用详解

目录

一,定义

二,绘制自定义多边形

三,作为其他组件的背景使

一,定义

Polygon是鸿蒙提供的多边形绘制组件,利用该组件可以绘制多边形背景,多边形图案等

官方提供的参数和属性:

参数:

参数名 参数类型 必填 默认值 参数描述
width string | number 0 宽度。 说明: 异常值按照默认值处理。
height string | number 0 高度。 说明: 异常值按照默认值处理。

属性:

名称 类型 默认值 描述
points Array<Point> [] 多边形的顶点坐标列表。 从API version 9开始,该接口支持在ArkTS卡片中使用。 说明: 异常值按照默认值处理。
fill ResourceColor Color.Black 设置填充区域颜色。 从API version 9开始,该接口支持在ArkTS卡片中使用。 说明: 异常值按照默认值处理。
fillOpacity Length 1 设置填充区域透明度。 取值范围是[0.0, 1.0],若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理。 从API version 9开始,该接口支持在ArkTS卡片中使用。
stroke ResourceColor - 设置边框颜色,不设置时,默认没有边框线条。 从API version 9开始,该接口支持在ArkTS卡片中使用。 说明: 异常值不会绘制边框线条。
strokeDashArray Array<Length> [] 设置边框间隙。 从API version 9开始,该接口支持在ArkTS卡片中使用。 说明: 线段相交时可能会出现重叠现象。异常值按照默认值处理。
strokeDashOffset number | string 0 边框绘制起点的偏移量。 从API version 9开始,该接口支持在ArkTS卡片中使用。 说明: 异常值按照默认值处理。
strokeLineCap LineCapStyle LineCapStyle.Butt 设置边框端点绘制样式。 从API version 9开始,该接口支持在ArkTS卡片中使用。
strokeLineJoin LineJoinStyle LineJoinStyle.Miter 设置边框拐角绘制样式。 从API version 9开始,该接口支持在ArkTS卡片中使用。
strokeMiterLimit number | string 4 设置斜接长度与边框宽度比值的极限值。斜接长度表示外边框外边交点到内边交点的距离,边框宽度即strokeWidth属性的值。 说明: 该属性取值需在strokeLineJoin属性取值LineJoinStyle.Miter时生效。 该属性的合法值范围应当大于等于1.0,当取值范围在[0,1)时按1.0处理,其余异常值按默认值处理。 从API version 9开始,该接口支持在ArkTS卡片中使用。
strokeOpacity Length 1 设置边框透明度。 说明: 该属性的取值范围是[0.0, 1.0],若给定值小于0.0,则取值为0.0;若给定值大于1.0,则取值为1.0,其余异常值按1.0处理 。 从API version 9开始,该接口支持在ArkTS卡片中使用。
strokeWidth Length 1 设置边框宽度。 从API version 9开始,该接口支持在ArkTS卡片中使用。 说明: 该属性若为string类型, 暂不支持百分比,百分比按照1px处理。
antiAlias boolean true 是否开启抗锯齿效果。 从API version 9开始,该接口支持在ArkTS卡片中使用。

Point:

名称 类型定义 描述
Point [number, number] 第一个参数为x轴坐标,第二个参数为y轴坐标(相对坐标)。

二,绘制自定义多边形

绘制一个五边形:

TypeScript 复制代码
@Entry
@Component
struct Index {

  build() {
      Stack({alignContent: Alignment.Center}) {
       
        Polygon({width: 200, height: 200})
          .points([[100, 0], [0, 100], [40, 200], [160, 200], [200, 100]])
          .fill("#ff1122")
      }
      .width("100%")
      .height("100%")

  }
}

绘制多边形边框并设置宽度:

TypeScript 复制代码
@Entry
@Component
struct Index {
  build() {
      Stack({alignContent: Alignment.Center}) {
      
        Polygon({width: 200, height: 200})
          .points([[100, 0], [0, 100], [40, 200], [160, 200], [200, 100]])
          .fill("#ff1122")
          .stroke("#000000")
          .strokeWidth(10)
      }
      .width("100%")
      .height("100%")

  }
}

效果如下:

设置多边形的边框为虚线:

TypeScript 复制代码
@Entry
@Component
struct Index {
  build() {
      Stack({alignContent: Alignment.Center}) {
       
        Polygon({width: 200, height: 200})
          .points([[100, 0], [0, 100], [40, 200], [160, 200], [200, 100]])
          .fill("#ff1122")
          .stroke("#000000")
          .strokeWidth(10)
          .strokeDashArray([1,2])
      }
      .width("100%")
      .height("100%")

  }
}

效果如下:

三,作为其他组件的背景使用

在安卓中,可以自定义drawable-xml来作为其他组件的背景,在鸿蒙中,可以将Polygon作为其他组件的背景使用。

首先自定义Builder:

TypeScript 复制代码
@Builder bg() {
    Polygon({width: "100%", height: "100%"})
      .points([[100, 0], [0, 100], [40, 200], [160, 200], [200, 100]])
      .fill("#ff1122")
      .stroke("#000000")
      .strokeWidth(10)
      .strokeDashArray([1,2])
  }

然后在组件中使用:

TypeScript 复制代码
@Entry
@Component
struct Index {

  @Builder bg() {
    Polygon({width: "100%", height: "100%"})
      .points([[100, 0], [0, 100], [40, 200], [160, 200], [200, 100]])
      .fill("#ff1122")
      .stroke("#000000")
      .strokeWidth(10)
      .strokeDashArray([1,2])
  }


  build() {
      Stack({alignContent: Alignment.Center}) {
        Image($r('app.media.pic_bed_nurse_in'))
          .width(60)
          .height(60)
      }
      .width(200)
      .height(200)
      .background(this.bg())

  }
}

效果如下:

相关推荐
一只大侠的侠16 小时前
Flutter开源鸿蒙跨平台训练营 Day 10特惠推荐数据的获取与渲染
flutter·开源·harmonyos
御承扬1 天前
鸿蒙NDK UI之文本自定义样式
ui·华为·harmonyos·鸿蒙ndk ui
前端不太难1 天前
HarmonyOS 游戏上线前必做的 7 类极端场景测试
游戏·状态模式·harmonyos
大雷神1 天前
HarmonyOS智慧农业管理应用开发教程--高高种地--第29篇:数据管理与备份
华为·harmonyos
讯方洋哥1 天前
HarmonyOS App开发——关系型数据库应用App开发
数据库·harmonyos
巴德鸟1 天前
华为手机鸿蒙4回退到鸿蒙3到鸿蒙2再回退到EMUI11 最后关闭系统更新
华为·智能手机·harmonyos·降级·升级·回退·emui
一起养小猫1 天前
Flutter for OpenHarmony 实战_魔方应用UI设计与交互优化
flutter·ui·交互·harmonyos
一只大侠的侠1 天前
Flutter开源鸿蒙跨平台训练营 Day7Flutter+ArkTS双方案实现轮播图+搜索框+导航组件
flutter·开源·harmonyos
那就回到过去1 天前
VRRP协议
网络·华为·智能路由器·ensp·vrrp协议·网络hcip
相思难忘成疾1 天前
通向HCIP之路:第四步:边界网关路由协议—BGP(概念、配置、特点、常见问题及其解决方案)
网络·华为·hcip