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

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

[[439847]]

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

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

https://harmonyos.51cto.com

前言

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

效果图

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

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

代码文件结构

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

正文

一、角度渐变SweepGradient

1. 添加角度渐变页面

Tabs

Tabs:一种可以通过页签进行内容视图切换的容器组件,每个页签对应一个内容视图

参数:

barPosition:非必填,指定页签位置来创建Tabs容器组件

  • BarPosition.Start:vertical属性方法设置为true时,页签位于容器左侧;vertical属性方法设置为false时,页签位于容器顶部(默认)
  • BarPosition.End:vertical属性方法设置为true时,页签位于容器右侧;vertical属性方法设置为false时,页签位于容器底部

index:非必填,指定初次初始页签索引,参数类型为number,即直接填数字,默认值为0

controller:非必填,设置Tabs控制器,默认值为null,一般都直接不填的

对Tabs组件的控制器,用于控制Tabs组件进行页签切换

changeIndex(value: number): void:控制Tabs切换到指定页签,index: 页签在Tabs里的索引值,索引值从0开始

属性:

vertical:是否为纵向Tab,参数类型为boolean,默认值为false

scrollable:是否可以通过左右滑动进行页面切换,参数类型为boolean,默认值为true

barMode:TabBar布局模式

BarMode.Scrollable:TabBar使用实际布局宽度, 超过总长度后可滑动

BarMode.Fixed:所有TabBar平均分配宽度

barWidth:TabBar的宽度值,参数类型为number,即直接填数字

barHeight:TabBar的高度值,参数类型为number,即直接填数字

animationDuration:TabContent滑动动画时长,参数类型为number,默认值为200

事件:

onChange(callback: (index: number) => void):Tab页签切换后触发的事件

TabContent

TabContent:仅在Tabs中使用,对应一个切换页签的内容视图

属性:

tabBar:设置TabBar上显示内容,参数类型:string

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

SweepGradient.ets:

  1. import { setreturnButton } from '../util/setreturnButton.ets'
  2. import { setcolorPlate, setSliderPoint } from '../util/setcolorPlate.ets' 
  3.  
  4. @Entry 
  5. @Component 
  6. struct SweepGradient { 
  7.   private controller: TabsController = new TabsController() 
  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 PointX:number = 165 
  15.   @State PointY:number = 165 
  16.  
  17.   build() { 
  18.     Column(){ 
  19.       setreturnButton() 
  20.  
  21.       Tabs({ barPosition: BarPosition.Start, index: 1, controller: this.controller }) { 
  22.         TabContent() { 
  23.           setcolorPlate({ strColor:$Color1, str:'一', inSetValue:$Point1 }) 
  24.         } 
  25.         .tabBar('颜色一'
  26.         .borderColor('#A168FE'
  27.         .borderWidth(2) 
  28.  
  29.         TabContent() { 
  30.           setcolorPlate({ strColor:$Color2, str:'二', inSetValue:$Point2 }) 
  31.         } 
  32.         .tabBar('颜色二'
  33.         .borderColor('#A168FE'
  34.         .borderWidth(2) 
  35.  
  36.         TabContent() { 
  37.           setcolorPlate({ strColor:$Color3, str:'三', inSetValue:$Point3 }) 
  38.         } 
  39.         .tabBar('颜色三'
  40.         .borderColor('#A168FE'
  41.         .borderWidth(2) 
  42.       } 
  43.       .vertical(false
  44.       .scrollable(true
  45.       .barMode(BarMode.Fixed) 
  46.       .barWidth(330) 
  47.       .barHeight(50) 
  48.       .animationDuration(400) 
  49.       .width('98%'
  50.       .height(260) 
  51.       .borderRadius(10) 
  52.       .borderColor('#A168FE'
  53.       .borderWidth(2) 
  54.  
  55.       setSliderPoint({ str:'X坐标'max:330, number:$PointX }) 
  56.       setSliderPoint({ str:'Y坐标'max:330, number:$PointY }) 
  57.  
  58.       Flex(){} 
  59.       .width(330) 
  60.       .height(330) 
  61.       .margin(10) 
  62.       .sweepGradient({ 
  63.         center: [this.PointX, this.PointY], 
  64.         start: 0, 
  65.         end: 359, 
  66.         colors: [[this.Color1, this.Point1], [this.Color2, this.Point2], [this.Color3, this.Point3]] 
  67.       }) 
  68.     } 
  69.     .width('100%'
  70.     .height('100%'
  71.   } 

二、径向渐变RadialGradient

1. 添加径向渐变页面

List

List:列表包含一系列相同宽度的列表项。适合连续、多行呈现同类数据,例如图片和文本

参数:

space:非必填,列表项间距,参数类型为number,即直接填数字,默认值为0

initialIndex:非必填,设置当前List初次加载时视口起始位置显示的item,即显示第一个item,如设置的序号超过了最后一个item的序号,则设置不生效,参数类型为number,即直接填数字,默认值为0

属性:

listDirection:设置List组件排列方向

  • Axis.Vertical:纵向排列(默认)
  • Axis.Horizontal:横向排列

editMode:声明当前List组件是否处于可编辑模式,参数类型为boolean,默认值为false,当设置了true时,则为可以删除该列表项

edgeEffect:滑动效果

  • EdgeEffect.Spring:弹性物理动效,滑动到边缘后可以根据初始速度或通过触摸事件继续滑动一段距离,松手后回弹(默认)
  • EdgeEffect.None:滑动到边缘后无效果

chainAnimation:用于设置当前list是否启用链式联动动效,开启后列表滑动以及顶部和底部拖拽时会有链式联动的效果。链式联动效果:list内的list-item间隔一定距离,在基本的滑动交互行为下,主动对象驱动从动对象进行联动,驱动效果遵循弹簧物理动效。参数类型为boolean,默认值为false

事件:

onItemDelete(index: number) => boolean:列表项删除时触发

onScrollIndex(firstIndex: number, lastIndex: number) => void:当前列表显示的起始位置和终止位置发生变化时触发

ListItem

ListItem:用来展示列表具体item,宽度默认充满List组件,必须配合List来使用

属性:

sticky:设置ListItem吸顶效果

  • Sticky.None:无吸顶效果(默认)
  • Sticky.Normal:当前item吸顶,滑动消失

editable:声明当前ListItem元素是否可编辑,进入编辑模式后可删除,参数类型为boolean,默认值为false

Scroll

Scroll:可滚动的容器组件,当子组件的布局尺寸超过父组件的视口时,内容可以滚动

属性:

scrollable:设置滚动方法

  • ScrollDirection.Horizontal:仅支持水平方向滚动
  • ScrollDirection.Vertical:仅支持竖直方向滚动(默认)
  • ScrollDirection.None:不可滚动

scrollBar:设置滚动条状态

  • Auto.Off:不显示
  • Auto.On:常驻显示
  • Auto.Auto:按需显示(触摸时显示,2s后消失)(默认)

scrollBarColor:设置滚动条的颜色,参数类型为Color

scrollBarWidth:设置滚动条的宽度,参数类型为Length

事件:

onScroll(xOffset: number, yOffset: number) => void:滚动事件回调, 返回滚动时水平、竖直方向偏移量

onScrollEdge(side: Edge) => void:滚动到边缘事件回调

onScrollEnd() => void:滚动停止时事件回调

对于可滚动容器组件的控制器,可以将此组件绑定至容器组件,然后通过它控制容器组件的滚动

scroller.scrollTo:滑动到指定位置

  • xOffset:必填,水平滑动偏移,参数类型为Length
  • yOffset:必填,竖直滑动偏移,参数类型为Length
  • animation:非必填,动画配置:duration: 滚动时长设置;curve: 滚动曲线设置

scroller.scrollEdge:滚动到容器边缘

  • value:必填,指定滚动到的边缘位置,参数类型为Edge

scroller.scrollPage:滚动到下一页或者上一页

  • next:必填,是否向下翻页,参数类型为boolean,true表示向下翻页,false表示向上翻页

scroller.currentOffset:返回当前的滚动偏移量,返回值xOffset: number水平滑动偏移,yOffset: number竖直滑动偏移

scroller.scrollToIndex:滑动到指定Index

  • value:必填,要滑动到的列表项在列表中的索引值,参数类型为number,即直接填数字

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

RadialGradient.ets:

  1. import { setreturnButton } from '../util/setreturnButton.ets'
  2. import { setcolorPlate, setSliderPoint } from '../util/setcolorPlate.ets' 
  3.  
  4. @Entry 
  5. @Component 
  6. struct RadialGradient { 
  7.   scroller: Scroller = new Scroller() 
  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 PointX:number = 165 
  15.   @State PointY:number = 165 
  16.   @State Radius:number = 165 
  17.  
  18.   build() { 
  19.     Scroll(this.scroller){ 
  20.       Column(){ 
  21.         setreturnButton() 
  22.  
  23.         List({ space: 10, initialIndex: 0 }) { 
  24.           ListItem() { 
  25.             setcolorPlate({ strColor:$Color1, str:'一', inSetValue:$Point1 }) 
  26.           } 
  27.           .borderRadius(10) 
  28.           .borderColor('#A168FE'
  29.           .borderWidth(2) 
  30.  
  31.           ListItem() { 
  32.             setcolorPlate({ strColor:$Color2, str:'二', inSetValue:$Point2 }) 
  33.           } 
  34.           .borderRadius(10) 
  35.           .borderColor('#A168FE'
  36.           .borderWidth(2) 
  37.  
  38.           ListItem() { 
  39.             setcolorPlate({ strColor:$Color3, str:'三', inSetValue:$Point3 }) 
  40.           } 
  41.           .borderRadius(10) 
  42.           .borderColor('#A168FE'
  43.           .borderWidth(2) 
  44.         } 
  45.         .listDirection(Axis.Vertical) 
  46.         .divider({ strokeWidth: 2, color: '#808080', startMargin: 10, endMargin: 10 }) 
  47.         .edgeEffect(EdgeEffect.None) 
  48.         .chainAnimation(true
  49.         .editMode(false
  50.         .width('98%'
  51.         .height(500) 
  52.         .padding({ top:5, bottom:5 }) 
  53.  
  54.         setSliderPoint({ str:'X坐标'max:330, number:$PointX }) 
  55.         setSliderPoint({ str:'Y坐标'max:330, number:$PointY }) 
  56.         setSliderPoint({ str:'半径'max:330, number:$Radius }) 
  57.  
  58.         Flex(){} 
  59.         .width(330) 
  60.         .height(330) 
  61.         .margin(10) 
  62.         .radialGradient({ 
  63.           center: [this.PointX, this.PointY], 
  64.           radius: this.Radius, 
  65.           colors: [[this.Color1, this.Point1], [this.Color2, this.Point2], [this.Color3, this.Point3]] 
  66.         }) 
  67.       } 
  68.     } 
  69.     .width('100%'
  70.     .height('100%'
  71.     .scrollable(ScrollDirection.Vertical) 
  72.     .scrollBar(BarState.Auto) 
  73.     .scrollBarColor(Color.Gray) 
  74.     .scrollBarWidth(30) 
  75.   } 

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

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

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

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

https://harmonyos.51cto.com

 

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

2021-12-10 15:02:47

鸿蒙HarmonyOS应用

2021-12-10 15:05:41

鸿蒙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项目开发鸿蒙

2021-12-01 15:40:23

鸿蒙HarmonyOS应用

2023-04-02 10:06:24

组件vue3sign2.

2015-07-22 15:19:46

Docker云计算微服务

2023-12-29 08:37:59

2022-03-17 16:04:16

Text文本组件Button组件Column

2022-02-17 14:51:39

HarmonyOSJSPAI开发canvas画布

2018-07-25 17:27:47

华为

2022-03-14 15:36:34

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

2011-04-22 10:13:35

SimpleFrame
点赞
收藏

51CTO技术栈公众号