自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于ArkUI的漸變色盤—容器組件的學(xué)習(xí)分享(中)

系統(tǒng)
目前HarmonyOS ArkUI 3.0框架的容器組件共有21個(gè),在學(xué)習(xí)完這21個(gè)容器組件后,打算使用盡可能多的容器組件基于HarmonyOS ArkUI 3.0框架去完成一個(gè)實(shí)踐開發(fā)。

[[439849]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

前言

【木棉花】基于ArkUI的漸變色盤——容器組件的學(xué)習(xí)分享(上)

效果圖

歡迎頁面線性漸變角度添加了漸變徑向漸變

【木棉花】基于ArkUI的漸變色盤——容器組件的學(xué)習(xí)分享(中)-鴻蒙HarmonyOS技術(shù)社區(qū)【木棉花】基于ArkUI的漸變色盤——容器組件的學(xué)習(xí)分享(中)-鴻蒙HarmonyOS技術(shù)社區(qū)【木棉花】基于ArkUI的漸變色盤——容器組件的學(xué)習(xí)分享(中)-鴻蒙HarmonyOS技術(shù)社區(qū)【木棉花】基于ArkUI的漸變色盤——容器組件的學(xué)習(xí)分享(中)-鴻蒙HarmonyOS技術(shù)社區(qū)

代碼文件結(jié)構(gòu)

【木棉花】基于ArkUI的漸變色盤——容器組件的學(xué)習(xí)分享(中)-鴻蒙HarmonyOS技術(shù)社區(qū)

正文

一、線性漸變LinearGradient

1. 添加顏色盤組件

從效果圖可以看出顏色盤的樣式是一致的,因此我們可以使用裝飾器@Component自定義顏色盤。在顏色盤中RGB的三個(gè)滑動(dòng)條的樣式也是一致的,所以同樣也可以使用裝飾器@Component自定義RGB顏色滑動(dòng)條。

  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){//返回十六進(jìn)制的字符串 
  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){//十進(jìn)制轉(zhuǎn)十六進(jìn)制的函數(shù) 
  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,//當(dāng)前進(jìn)度條 
  42.         min: 0,//設(shè)置最小值 
  43.         max: 255,//設(shè)置最大值 
  44.         step: 1,//設(shè)置Slider滑動(dòng)跳動(dòng)值,當(dāng)設(shè)置相應(yīng)的step時(shí),Slider為間歇滑動(dòng)。 
  45.         style: SliderStyle.OutSet//設(shè)置Slider的滑塊樣式,SliderStyle.OutSet表示滑塊在滑軌上,SliderStyle.InSet表示滑塊在滑軌內(nèi) 
  46.       }) 
  47.         .width('80%'
  48.         .blockColor(0xCCCCCC)//設(shè)置滑塊的顏色 
  49.         .trackColor(Color.Black)//設(shè)置滑軌的背景顏色 
  50.         .selectedColor(this.color)//設(shè)置滑軌的已滑動(dòng)顏色 
  51.         .showSteps(false)//設(shè)置當(dāng)前是否顯示步長刻度值 
  52.         .showTips(false)//設(shè)置滑動(dòng)時(shí)是否顯示氣泡提示百分比 
  53.         .onChange((value: number) => {//Slider滑動(dòng)時(shí)觸發(fā)事件回調(diào) 
  54.           this.inSetValue = value//value:當(dāng)前進(jìn)度值 
  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:新事件標(biāo)記組件,在組件上提供事件信息展示能力

參數(shù):

根據(jù)數(shù)值創(chuàng)建提醒組件

count:必填,設(shè)置提醒消息數(shù),參數(shù)類型為:number

position:非必填,設(shè)置提示點(diǎn)顯示位置

  • BadgePosition.Right:圓點(diǎn)顯示在右側(cè)縱向居中
  • BadgePosition.RightTop:圓點(diǎn)顯示在右上角(默認(rèn))
  • BadgePosition.Left:圓點(diǎn)顯示在左側(cè)縱向居中

maxCount:非必填,最大消息數(shù),超過最大消息時(shí)僅顯示maxCount+,參數(shù)類型為number,即直接填數(shù)字,默認(rèn)值為99

style:非必填,Badge組件可設(shè)置樣式,支持設(shè)置文本顏色、尺寸、圓點(diǎn)顏色和尺寸

  • color:非必填,文本顏色,參數(shù)類型為Color,默認(rèn)值為Color.White
  • fontSize:非必填,文本大小,參數(shù)類型為number或string,默認(rèn)值為10
  • badgeSize:必填,badge的大小,參數(shù)類型為number或string
  • badgeColor:非必填,badge的顏色,參數(shù)類型為Color,默認(rèn)值為Color.Red

根據(jù)字符串創(chuàng)建提醒組件

value:必填,提示內(nèi)容的文本字符串,參數(shù)類型為:string

position:非必填,設(shè)置提示點(diǎn)顯示位置

  • BadgePosition.Right:圓點(diǎn)顯示在右側(cè)縱向居中
  • BadgePosition.RightTop:圓點(diǎn)顯示在右上角(默認(rèn))
  • BadgePosition.Left:圓點(diǎn)顯示在左側(cè)縱向居中

style:非必填,Badge組件可設(shè)置樣式,支持設(shè)置文本顏色、尺寸、圓點(diǎn)顏色和尺寸

  • color:非必填,文本顏色,參數(shù)類型為Color,默認(rèn)值為Color.White
  • fontSize:非必填,文本大小,參數(shù)類型為number或string,默認(rèn)值為10
  • badgeSize:必填,badge的大小,參數(shù)類型為number或string
  • badgeColor:非必填,badge的顏色,參數(shù)類型為Color,默認(rèn)值為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({//顏色斷點(diǎn)位置的滑動(dòng)條 
  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.   } 

同時(shí)注意到在三種顏色漸變的模式中,還需要方向、位置、半徑等的滑動(dòng)條,由于不是所有漸變模式中都有的,所以需要另外使用裝飾器@Component自定義滑動(dòng)條。

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,//不同漸變模式下需要的滑動(dòng)條最大值不一致,所以需要設(shè)為變量 
  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:指定跳轉(zhuǎn)目標(biāo)頁面的路徑,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)//當(dāng)前路由組件是否處于激活狀態(tài),處于激活狀態(tài)時(shí),會(huì)生效相應(yīng)的路由操作 
  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. }*/ 

注意到在線性漸變中有兩個(gè)按鈕,按鈕中除了文本不一致之外,其他樣式都一樣,所以使用裝飾器@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:滑動(dòng)容器,提供切換子組件顯示的能力

參數(shù):

  • controller:非必填,給組件綁定一個(gè)控制器,用來控制組件翻頁,默認(rèn)值為null,一般都直接不填的

對(duì)Swiper容器組件的控制器,可以將此對(duì)象綁定至Swiper組件,然后通過它控制翻頁

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

屬性:

  • index:設(shè)置當(dāng)前在容器中顯示的子組件的索引值,參數(shù)類型為number,默認(rèn)值為0
  • autoPlay:子組件是否自動(dòng)播放,自動(dòng)播放狀態(tài)下,導(dǎo)航點(diǎn)不可操作,參數(shù)類型為boolean,默認(rèn)值為false
  • interval:使用自動(dòng)播放時(shí)播放的時(shí)間間隔,單位為毫秒,參數(shù)類型為number,默認(rèn)值為3000
  • indicator:是否啟用導(dǎo)航點(diǎn)指示器,參數(shù)類型為boolean,默認(rèn)值為true
  • loop:是否開啟循環(huán),參數(shù)類型為boolean,默認(rèn)值為true
  • duration:子組件切換的動(dòng)畫時(shí)長,單位為毫秒,參數(shù)類型為number,默認(rèn)值為400
  • vertical:是否為縱向滑動(dòng),參數(shù)類型為boolean,默認(rèn)值為false
  • itemSpace:設(shè)置子組件與子組件之間間隙,參數(shù)類型為Length,默認(rèn)值為0

事件:

  • onChange( index: number) => void:當(dāng)前顯示的組件索引變化時(shí)觸發(fā)該事件

通過 “import {結(jié)構(gòu)體名} 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:'上一個(gè)顏色', swiperController:this.swiperController }) 
  37.         setSliderButton({ str:'下一個(gè)顏色', 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 == '上一個(gè)顏色'){ 
  73.         this.swiperController.showPrevious()//翻至上一頁 
  74.       }else
  75.         this.swiperController.showNext()//翻至下一頁 
  76.       } 
  77.     }) 
  78.   } 

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載

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

想了解更多內(nèi)容,請(qǐng)?jiān)L問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-12-10 15:02:47

鴻蒙HarmonyOS應(yīng)用

2021-12-10 15:04:24

鴻蒙HarmonyOS應(yīng)用

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心電圖組件

2024-07-31 20:38:18

2022-10-17 14:36:09

ArkUI虛擬搖桿組件

2022-07-26 14:40:42

ArkUIJS

2022-10-19 15:12:05

ArkUI鴻蒙

2022-09-15 15:04:16

ArkUI鴻蒙

2022-03-10 14:57:35

ArkUIets項(xiàng)目開發(fā)鴻蒙

2015-07-22 15:19:46

Docker云計(jì)算微服務(wù)

2023-04-02 10:06:24

組件vue3sign2.

2023-12-29 08:37:59

2018-07-25 17:27:47

華為

2012-05-31 10:57:06

HTML5

2022-02-17 14:51:39

HarmonyOSJSPAI開發(fā)canvas畫布

2022-03-14 15:36:34

Row容器組件Column容器組件鴻蒙

2011-04-22 10:13:35

SimpleFrame
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)