基于ArkUI的渐变色盘—容器组件的学习分享(中)

系统
目前HarmonyOS ArkUI 3.0框架的容器组件共有21个,在学习完这21个容器组件后,打算使用尽可能多的容器组件基于HarmonyOS ArkUI 3.0框架去完成一个实践开发。

[[439849]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

前言

【木棉花】基于ArkUI的渐变色盘——容器组件的学习分享(上)

效果图

欢迎页面线性渐变角度添加了渐变径向渐变

【木棉花】基于ArkUI的渐变色盘——容器组件的学习分享(中)-鸿蒙HarmonyOS技术社区【木棉花】基于ArkUI的渐变色盘——容器组件的学习分享(中)-鸿蒙HarmonyOS技术社区【木棉花】基于ArkUI的渐变色盘——容器组件的学习分享(中)-鸿蒙HarmonyOS技术社区【木棉花】基于ArkUI的渐变色盘——容器组件的学习分享(中)-鸿蒙HarmonyOS技术社区

代码文件结构

【木棉花】基于ArkUI的渐变色盘——容器组件的学习分享(中)-鸿蒙HarmonyOS技术社区

正文

一、线性渐变LinearGradient

1. 添加颜色盘组件

从效果图可以看出颜色盘的样式是一致的,因此我们可以使用装饰器@Component自定义颜色盘。在颜色盘中RGB的三个滑动条的样式也是一致的,所以同样也可以使用装饰器@Component自定义RGB颜色滑动条。

  1. @Component 
  2. struct setSlider{ 
  3.   @State inSetValue: number = 128 
  4.   @Link str: string 
  5.   private text: string 
  6.   private color: string 
  7.  
  8.   sexadecimal(number){//返回十六进制的字符串 
  9.     let num1 = Math.floor(number / 16) 
  10.     let num2 = Math.ceil(number - num1 * 16) 
  11.  
  12.     return this.takeCharacter(num1) + this.takeCharacter(num2) 
  13.   } 
  14.  
  15.   takeCharacter(number){//十进制转十六进制的函数 
  16.     if(number < 10){ 
  17.       return number.toString() 
  18.     }else if(number == 10){ 
  19.       return 'A' 
  20.     }else if(number == 11){ 
  21.       return 'B' 
  22.     }else if(number == 12){ 
  23.       return 'C' 
  24.     }else if(number == 13){ 
  25.       return 'D' 
  26.     }else if(number == 14){ 
  27.       return 'E' 
  28.     }else
  29.       return 'F' 
  30.     } 
  31.   } 
  32.  
  33.   build(){ 
  34.     Row(){ 
  35.       Text(this.text + ':'
  36.         .width('8%'
  37.         .fontSize(22) 
  38.         .fontColor(this.color) 
  39.  
  40.       Slider({ 
  41.         value: this.inSetValue,//当前进度条 
  42.         min: 0,//设置最小值 
  43.         max: 255,//设置最大值 
  44.         step: 1,//设置Slider滑动跳动值,当设置相应的step时,Slider为间歇滑动。 
  45.         style: SliderStyle.OutSet//设置Slider的滑块样式,SliderStyle.OutSet表示滑块在滑轨上,SliderStyle.InSet表示滑块在滑轨内 
  46.       }) 
  47.         .width('80%'
  48.         .blockColor(0xCCCCCC)//设置滑块的颜色 
  49.         .trackColor(Color.Black)//设置滑轨的背景颜色 
  50.         .selectedColor(this.color)//设置滑轨的已滑动颜色 
  51.         .showSteps(false)//设置当前是否显示步长刻度值 
  52.         .showTips(false)//设置滑动时是否显示气泡提示百分比 
  53.         .onChange((value: number) => {//Slider滑动时触发事件回调 
  54.           this.inSetValue = value//value:当前进度值 
  55.           this.str = this.sexadecimal(value) 
  56.         }) 
  57.  
  58.       Text(this.inSetValue.toFixed(0)) 
  59.         .width('12%'
  60.         .fontSize(22) 
  61.         .fontColor(this.color) 
  62.     } 
  63.   } 

Badge

Badge:新事件标记组件,在组件上提供事件信息展示能力

参数:

根据数值创建提醒组件

count:必填,设置提醒消息数,参数类型为:number

position:非必填,设置提示点显示位置

  • BadgePosition.Right:圆点显示在右侧纵向居中
  • BadgePosition.RightTop:圆点显示在右上角(默认)
  • BadgePosition.Left:圆点显示在左侧纵向居中

maxCount:非必填,最大消息数,超过最大消息时仅显示maxCount+,参数类型为number,即直接填数字,默认值为99

style:非必填,Badge组件可设置样式,支持设置文本颜色、尺寸、圆点颜色和尺寸

  • color:非必填,文本颜色,参数类型为Color,默认值为Color.White
  • fontSize:非必填,文本大小,参数类型为number或string,默认值为10
  • badgeSize:必填,badge的大小,参数类型为number或string
  • badgeColor:非必填,badge的颜色,参数类型为Color,默认值为Color.Red

根据字符串创建提醒组件

value:必填,提示内容的文本字符串,参数类型为:string

position:非必填,设置提示点显示位置

  • BadgePosition.Right:圆点显示在右侧纵向居中
  • BadgePosition.RightTop:圆点显示在右上角(默认)
  • BadgePosition.Left:圆点显示在左侧纵向居中

style:非必填,Badge组件可设置样式,支持设置文本颜色、尺寸、圆点颜色和尺寸

  • color:非必填,文本颜色,参数类型为Color,默认值为Color.White
  • fontSize:非必填,文本大小,参数类型为number或string,默认值为10
  • badgeSize:必填,badge的大小,参数类型为number或string
  • badgeColor:非必填,badge的颜色,参数类型为Color,默认值为Color.Red

使用装饰器@Component自定义颜色盘。

setcolorPlate.ets:

  1. @Component 
  2. export struct setcolorPlate{ 
  3.   private str: number = 1 
  4.   @Link inSetValue: number 
  5.   @State strR: string = '80' 
  6.   @State strG: string = '80' 
  7.   @State strB: string = '80' 
  8.   @Link strColor: string 
  9.  
  10.   aboutToAppear(){ 
  11.     setInterval(() => {//返回RGB颜色 
  12.       this.strColor = '#' + this.strR + this.strG + this.strB 
  13.     }, 200) 
  14.   } 
  15.  
  16.   build(){ 
  17.     Column(){ 
  18.       Badge({ 
  19.         value: ''
  20.         style: { badgeSize: 20, badgeColor: '#' + this.strR + this.strG + this.strB } 
  21.       }){ 
  22.         Text('颜色' + this.str + ':#' + this.strR + this.strG + this.strB) 
  23.           .fontSize(30) 
  24.           .fontColor('#' + this.strR + this.strG + this.strB) 
  25.       } 
  26.       .width(280) 
  27.       .height(50) 
  28.  
  29.       setSlider({ str:$strR, text:'R', color:'#FF0000' }) 
  30.       setSlider({ str:$strG, text:'G', color:'#00FF00' }) 
  31.       setSlider({ str:$strB, text:'B', color:'#0000FF' }) 
  32.       Row(){ 
  33.         Slider({//颜色断点位置的滑动条 
  34.           value: this.inSetValue * 100, 
  35.           min: 0, 
  36.           max: 100, 
  37.           step: 1, 
  38.           style: SliderStyle.OutSet 
  39.         }) 
  40.           .width('80%'
  41.           .blockColor(0xCCCCCC) 
  42.           .trackColor(Color.Black) 
  43.           .selectedColor(this.strColor) 
  44.           .showSteps(false
  45.           .showTips(false
  46.           .onChange((value: number) => { 
  47.             this.inSetValue = parseFloat(value == 0 ? '0' : value == 100 ? '1' : '0.' + value) 
  48.           }) 
  49.  
  50.         Text(this.inSetValue.toString()) 
  51.           .width('14%'
  52.           .fontSize(24) 
  53.           .fontColor(this.strColor) 
  54.       } 
  55.     } 
  56.   } 

同时注意到在三种颜色渐变的模式中,还需要方向、位置、半径等的滑动条,由于不是所有渐变模式中都有的,所以需要另外使用装饰器@Component自定义滑动条。

setcolorPlate.ets:

  1. @Component 
  2. export struct setSliderPoint{ 
  3.   private str: string 
  4.   private max: number 
  5.   @Link number: number 
  6.  
  7.   build(){ 
  8.     Row(){ 
  9.       Text(this.str + ':'
  10.         .width('18%'
  11.         .fontSize(22) 
  12.         .fontColor('#A168FE'
  13.  
  14.       Slider({ 
  15.         value: this.number, 
  16.         min: 0, 
  17.         max: this.max,//不同渐变模式下需要的滑动条最大值不一致,所以需要设为变量 
  18.         step: 1, 
  19.         style: SliderStyle.OutSet 
  20.       }) 
  21.         .width('70%'
  22.         .blockColor(0xCCCCCC) 
  23.         .trackColor(Color.Black) 
  24.         .selectedColor('#A168FE'
  25.         .showSteps(false
  26.         .showTips(false
  27.         .onChange((value: number) => { 
  28.           this.number = value 
  29.         }) 
  30.  
  31.       Text(this.number.toFixed(0)) 
  32.         .width('12%'
  33.         .fontSize(22) 
  34.         .fontColor('#A168FE'
  35.     } 
  36.   } 

2. 添加返回主页按钮组件

通过Navigator容器组件为按钮Button添加路由功能。

setreturnButton.ets:

  1. //import router from '@system.router'
  2.  
  3. @Component 
  4. export struct setreturnButton{ 
  5.   @State active: boolean = false 
  6.  
  7.   build(){ 
  8.     Navigator({ target: '', type: NavigationType.Back }){//target:指定跳转目标页面的路径,NavigationType.Back:返回上一页面或指定的页面 
  9.       Button({ type: ButtonType.Normal, stateEffect: true }){ 
  10.         setText({ str:'返回主页' }) 
  11.       } 
  12.       .width(160) 
  13.       .height(60) 
  14.       .borderRadius(10) 
  15.       .borderColor('#A168FE'
  16.       .borderWidth(2) 
  17.       .backgroundColor('#DEB0DF'
  18.       .margin(10) 
  19.       .onClick(() => { 
  20.         this.active = true 
  21.       }) 
  22.     } 
  23.     .active(this.active)//当前路由组件是否处于激活状态,处于激活状态时,会生效相应的路由操作 
  24.   } 
  25.  
  26. /*@Component 
  27. export struct setreturnButton{ 
  28.   build(){ 
  29.     Button({ type: ButtonType.Normal, stateEffect: true }){ 
  30.       setText({ str:'返回主页' }) 
  31.     } 
  32.     .width(160) 
  33.     .height(60) 
  34.     .borderRadius(10) 
  35.     .borderColor('#A168FE'
  36.     .borderWidth(2) 
  37.     .backgroundColor('#DEB0DF'
  38.     .margin(10) 
  39.     .onClick(() => { 
  40.       router.back() 
  41.     }) 
  42.   } 
  43. }*/ 

注意到在线性渐变中有两个按钮,按钮中除了文本不一致之外,其他样式都一样,所以使用装饰器@Component自定义文本。

setreturnButton.ets:

  1. @Component 
  2. export struct setText{ 
  3.   private str: string 
  4.  
  5.   build(){ 
  6.     Text(this.str) 
  7.       .fontFamily('方正舒体'
  8.       .fontSize(32) 
  9.       .fontWeight(800) 
  10.       .fontColor('#FDEB82'
  11.   } 

 3. 添加线性渐变页面

Swiper

Swiper:滑动容器,提供切换子组件显示的能力

参数:

  • controller:非必填,给组件绑定一个控制器,用来控制组件翻页,默认值为null,一般都直接不填的

对Swiper容器组件的控制器,可以将此对象绑定至Swiper组件,然后通过它控制翻页

  • showNext():void:翻至下一页
  • showPrevious():void: 翻至上一页

属性:

  • index:设置当前在容器中显示的子组件的索引值,参数类型为number,默认值为0
  • autoPlay:子组件是否自动播放,自动播放状态下,导航点不可操作,参数类型为boolean,默认值为false
  • interval:使用自动播放时播放的时间间隔,单位为毫秒,参数类型为number,默认值为3000
  • indicator:是否启用导航点指示器,参数类型为boolean,默认值为true
  • loop:是否开启循环,参数类型为boolean,默认值为true
  • duration:子组件切换的动画时长,单位为毫秒,参数类型为number,默认值为400
  • vertical:是否为纵向滑动,参数类型为boolean,默认值为false
  • itemSpace:设置子组件与子组件之间间隙,参数类型为Length,默认值为0

事件:

  • onChange( index: number) => void:当前显示的组件索引变化时触发该事件

通过 “import {结构体名} from 路径名” 引入自定义组件。

LinearGradient.ets:

  1. import { setreturnButton, setText } from '../util/setreturnButton.ets'
  2. import { setcolorPlate, setSliderPoint } from '../util/setcolorPlate.ets' 
  3.  
  4. @Entry 
  5. @Component 
  6. struct LinearGradient { 
  7.   private swiperController: SwiperController = new SwiperController() 
  8.   @State Color1:string = '#808080' 
  9.   @State Point1:number = 0.1 
  10.   @State Color2:string = '#808080' 
  11.   @State Point2:number = 0.4 
  12.   @State Color3:string = '#808080' 
  13.   @State Point3:number = 0.8 
  14.   @State Angle:number = 180 
  15.  
  16.   build() { 
  17.     Column() { 
  18.       setreturnButton() 
  19.  
  20.       Swiper(this.swiperController) { 
  21.         setcolorPlate({ strColor:$Color1, str:'一', inSetValue:$Point1 }) 
  22.         setcolorPlate({ strColor:$Color2, str:'二', inSetValue:$Point2 }) 
  23.         setcolorPlate({ strColor:$Color3, str:'三', inSetValue:$Point3 }) 
  24.       } 
  25.       .width('100%'
  26.       .height(250) 
  27.       .borderRadius(10) 
  28.       .borderColor('#A168FE'
  29.       .borderWidth(2) 
  30.       .index(1) 
  31.       .autoPlay(false
  32.       .indicator(true
  33.       .loop(false
  34.  
  35.       setSliderPoint({ str:'Angle'max:359, number:$Angle }) 
  36.  
  37.       Flex(){} 
  38.       .width('90%'
  39.       .height(300) 
  40.       .margin(10) 
  41.       .linearGradient({ 
  42.         angle: this.Angle, 
  43.         colors: [[this.Color1, this.Point1], [this.Color2, this.Point2], [this.Color3, this.Point3]] 
  44.       }) 
  45.     } 
  46.     .width('100%'
  47.     .height('100%'
  48.   } 

最后使用装饰器@Component自定义翻页按钮组件。

LinearGradient.ets:

  1. import { setreturnButton, setText } from '../util/setreturnButton.ets'
  2. import { setcolorPlate, setSliderPoint } from '../util/setcolorPlate.ets' 
  3.  
  4. @Entry 
  5. @Component 
  6. struct LinearGradient { 
  7.   private swiperController: SwiperController = new SwiperController() 
  8.   @State Color1:string = '#808080' 
  9.   @State Point1:number = 0.1 
  10.   @State Color2:string = '#808080' 
  11.   @State Point2:number = 0.4 
  12.   @State Color3:string = '#808080' 
  13.   @State Point3:number = 0.8 
  14.   @State Angle:number = 180 
  15.  
  16.   build() { 
  17.     Column() { 
  18.       setreturnButton() 
  19.  
  20.       Swiper(this.swiperController) { 
  21.         setcolorPlate({ strColor:$Color1, str:'一', inSetValue:$Point1 }) 
  22.         setcolorPlate({ strColor:$Color2, str:'二', inSetValue:$Point2 }) 
  23.         setcolorPlate({ strColor:$Color3, str:'三', inSetValue:$Point3 }) 
  24.       } 
  25.       .width('100%'
  26.       .height(250) 
  27.       .borderRadius(10) 
  28.       .borderColor('#A168FE'
  29.       .borderWidth(2) 
  30.       .index(1) 
  31.       .autoPlay(false
  32.       .indicator(true
  33.       .loop(false
  34.  
  35.       Row(){ 
  36.         setSliderButton({ str:'上一个颜色', swiperController:this.swiperController }) 
  37.         setSliderButton({ str:'下一个颜色', swiperController:this.swiperController }) 
  38.       } 
  39.  
  40.       setSliderPoint({ str:'Angle'max:359, number:$Angle }) 
  41.  
  42.       Flex(){} 
  43.       .width('90%'
  44.       .height(300) 
  45.       .margin(10) 
  46.       .linearGradient({ 
  47.         angle: this.Angle, 
  48.         colors: [[this.Color1, this.Point1], [this.Color2, this.Point2], [this.Color3, this.Point3]] 
  49.       }) 
  50.     } 
  51.     .width('100%'
  52.     .height('100%'
  53.   } 
  54.  
  55. @Component 
  56. struct setSliderButton{ 
  57.   private str: string 
  58.   private swiperController: SwiperController 
  59.  
  60.   build(){ 
  61.     Button({ type: ButtonType.Normal, stateEffect: true }){ 
  62.       setText({ str:this.str }) 
  63.     } 
  64.     .width(160) 
  65.     .height(60) 
  66.     .borderRadius(10) 
  67.     .borderColor('#A168FE'
  68.     .borderWidth(2) 
  69.     .backgroundColor('#DEB0DF'
  70.     .margin(10) 
  71.     .onClick(() => { 
  72.       if(this.str == '上一个颜色'){ 
  73.         this.swiperController.showPrevious()//翻至上一页 
  74.       }else
  75.         this.swiperController.showNext()//翻至下一页 
  76.       } 
  77.     }) 
  78.   } 

文章相关附件可以点击下面的原文链接前往下载

https://harmonyos.51cto.com/resource/1573

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-12-10 15:02:47

鸿蒙HarmonyOS应用

2021-12-10 15:04:24

鸿蒙HarmonyOS应用

2024-01-16 08:22:42

Gradient线性梯度

2024-01-29 08:57:06

CSS渐变色前端

2023-05-06 07:23:57

2022-10-24 14:49:54

ArkUI心电图组件

2022-07-26 14:40:42

ArkUIJS

2022-10-17 14:36:09

ArkUI虚拟摇杆组件

2022-09-15 15:04:16

ArkUI鸿蒙

2022-10-19 15:12:05

ArkUI鸿蒙

2022-03-10 14:57:35

ArkUIets项目开发鸿蒙

2023-04-02 10:06:24

组件vue3sign2.

2023-12-29 08:37:59

2015-07-22 15:19:46

Docker云计算微服务

2022-03-14 15:36:34

Row容器组件Column容器组件鸿蒙

2022-03-17 16:04:16

Text文本组件Button组件Column

2018-07-25 17:27:47

华为

2022-02-17 14:51:39

HarmonyOSJSPAI开发canvas画布

2021-12-20 20:30:48

鸿蒙HarmonyOS应用

2022-09-16 15:34:32

CanvasArkUI
点赞
收藏

51CTO技术栈公众号