UI界面是应用程序可视化必不可少的部分。设计精致的UI界面可以使得整个可视化应用程序给用户留下深刻的印象,是改善用户界面体验最直接的方式。

ArkUI开发框架为开发者提供了丰富的UI原生组件,如Navigation(Page页面的根容器)、ScrollBar(滚动条组件)、Swiper(滑动容器)及Tabs(通过页签进行内容视图切换的容器组件)等。其中,Swiper组件和Tabs组件在应用程序开发中对于指示器的使用引入较多,但是直接使用原生的Swiper组件和Tabs组件不适用于表现复杂的UI指示器交互变化。因此,我们针对Swiper组件和Tabs组件在指示器应用方向做了一个简单的封装,以CiecleIndicator三方组件的形式提供应用程序依赖使用,从而提升了ArkUI开发框架的UI界面之指示器风格多样化的能力。

CircleIndicator介绍

CircleIndicator组件UI效果展示

圆形指示器:

长条指示器:

横幅指示器:

三角指示器:

图标指示器:

携带中央视图的Tabs指示器:

固定位置Tabs指示器:

固定位置Tabs指示器(胶囊风格):

固定位置Tabs指示器(携带角标):

可滑动Tabs指示器:

可滑动Tabs指示器(水滴滑块):

可滑动Tabs指示器(首列固定):

titles指示器:

什么是CircleIndicator?

CircleIndicator顾名思义,它指的是圆形指示器。不过在我们OpenHarmony三方组件中的CircleIndicator组件不再是狭义的圆形指示器,而是将多种表现形式的指示器汇集为一体的归一化指示器合集组件。

CircleIndicator的源码实现

这里我们以CircleIndicator组件源码中的TriangularIndicator.ets文件为源码解析样例对象展开分析。首先创建TriangularIndicator.ets文件,使用命名空间建立TriangularIndicator初始化模型:

namespace TriangularIndicator {
export class Model {
//设置指示器高度
mHeight: number = 18
//设置指示器宽度
mWidth: number = lpx2px(720)
//设置指示器背景色
mBackgroundColor: string
//字段过多,此处进行省略
//各字段set与get方法,此处只以height字段为例
public getHeight(): number {
return this.mHeight
}
public setHeight(height: number): Model {
this.mHeight = height
return this
}
//触摸事件拦截
onPageTouch: (event: TouchEvent, currentIndicator: number) => void
public notifyTouch(event: TouchEvent, currentIndex: number) {
this.onPageTouch(event, currentIndex)
}
//设置构造器
private tabsController: TabsController
(tabsController: TabsController) {
this.tabsController = tabsController
}
//页面切换监听
indexChange: (itemIndex: number) => void
public setChangeListener(callback: (index: number) => void): Model{
this.indexChange = callback
return this
}
}

  

将TriangularIndicator应用组件化:

@Component
struct TriangularIndicator {
//获取TriangularIndicator实例
@State model: TriangularIndicator.Model = new TriangularIndicator.Model(null)
//初始化指示器当前index
@Link @Watch("itemIndexChange") itemIndex: number
//设置指示器总条数
@State count: number = 0
//再分别实现itemIndexChange、aboutToAppear、onTouch、getOffset方法,此处实现不做展示
//再在build方法里面描述UI结构
build() {
Column() {
Rect({ width: this.model.mWidth, height: this.model.mLineHeight }).fill(this.model.mLineColor)
Polygon()
.points(this.model.mReverse ?
[[px2vp(this.model.mWidth) / (this.count * 2) - this.model.mTriangleWidth / 2, this.model.mLineHeight - this.model.mYOffset],
[px2vp(this.model.mWidth) / (this.count * 2), this.model.mLineHeight + this.model.mTriangleHeight - this.model.mYOffset],
[px2vp(this.model.mWidth) / (this.count * 2) + this.model.mTriangleWidth / 2, this.model.mLineHeight - this.model.mYOffset]] :
[[px2vp(this.model.mWidth) / (this.count * 2) - this.model.mTriangleWidth / 2, -this.model.mYOffset],
[px2vp(this.model.mWidth) / (this.count * 2), -this.model.mTriangleHeight - this.model.mYOffset],
[px2vp(this.model.mWidth) / (this.count * 2) + this.model.mTriangleWidth / 2, -this.model.mYOffset]])
.offset(this.model.mStartInterpolator ?
{ x: px2vp(this.model.mWidth) / this.count * (this.itemIndex - this.model.mStartInterpolator.interpolate(Math.abs(this.model.offs et / this.model.mWidth)) * Math.sign(this.model.offset)),
y: 0 } :
{ x: px2vp(this.model.mWidth) / this.count * (this.itemIndex - this.model.offset / this.model.mWidth),
y: 0 })
.fill(this.model.mLineColor)
.height(this.model.mHeight)
.width(this.model.mWidth)
}.width('100%').height(this.model.mHeight)
.backgroundColor(this.model.mBackgroundColor)
}
}

  

最后将TriangularIndicator暴露出来供外部引用:

export default TriangularIndicator

  

CircleIndicator实战

创建项目

如图所示,在DevEco Studio中新建CircleIndicator_Test项目,项目类型选择Application,语言选择eTS,点击Finish完成创建。

创建工程

添加依赖

成功创建项目后,接下来就是将CircleIndicator组件下载至项目中。请在添加依赖之前参考如何安装OpenHarmony npm包(https://gitee.com/openharmony-tpc/docs/blob/master/OpenHarmony_npm_usage.md)完成OpenHarmony npm环境配置。完成OpenHarmony npm环境配置之后,在DevEco Studio的底部导航栏,点击“Terminal”(快捷键Alt+F12), 键入命令:npm install @ohos/circle-indicator --save并回车,此时CircleIndicator组件会自动下载至项目中。下载完成后工程根目录下会生成node_modules/@ohos/CircleIndicator目录。

编写逻辑代码

提供多种Indicator,使用方法类似,以TriangularIndicator为例

1. 初始化:实例化TabsController和对应的Indicator.Model 对象, 并添加number类型成员以记录当前页下标

private controller: TabsController = new TabsController()
@State model: TriangularIndicator.Model = new TriangularIndicator.Model(this.controller)
@State itemIndex: number = 0

  

2. 属性设置:通过Model类对象设置UI属性来自定义所需风格,也可以添加所需的回调

aboutToAppear() {
this.model
.setReverse(true)
.setLineHeight(4)
.setTriangleHeight(10)
.setLineColor("#e94220")
.setBackgroundColor("#eeeeee")
.setChangeListener((itemIndex) => {
console.info("change page to " + this.data[itemIndex])
})
}

  

3. 界面绘制:在Tabs组件旁放置Indicator组件,注意需要关闭原生的bar。并监听Tabs组件的touch事件,通知给Model类,以统一处理滑动逻辑

build() {
Column() {
TriangularIndicator({ itemIndex: $itemIndex, count: this.data.length, model: this.model })
Tabs({ index: this.itemIndex, controller: this.controller }) {
……
}
.barWidth(0)
.onTouch((event: TouchEvent) => {
this.model.notifyTouch(event, this.itemIndex)
})
}.padding({ top: 40 })
.backgroundColor("#eeeeee")
}

  

本期基于OpenHarmony API8的CircleIndicator组件1.0.3(https://gitee.com/openharmony-sig/CircleIndicator/tree/1.0.3)就为大家介绍到这,欢迎大家积极参与贡献。了解更多三方组件动态,请关注三方组件资源汇总(https://gitee.com/openharmony-tpc/tpc_resource),更多优秀的组件等你来发现!

CircleIndicator组件,使指示器风格更加多样化的更多相关文章

  1. 小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

    小程序scroll-view组件使用时,子元素虽设置样式display:inline-flex;whit-space:nowrap

  2. vue父子组件使用时遇到的一个问题

    子组件一定要写在父组件之前,例如: //子vue,这里遇到一个坑,那就是子vue一定要写在父vue前面,不然会报错. Vue.component('todo-item', { template: $( ...

  3. 前端使用mobx时,变量已经修改了,为什么组件还是没变化,map类型变量,对象类型变量的值获取问题(主要矛盾发生在组件使用时)

    前天我在使用一个前端多选框组件时遇到了一个问题,明明对象内的值已经修改了,但是组件显示的还是没有效果改变,以下是当时打出的log,我打印了这个对象的信息 对象内的值已经修改了但是组件还是不能及时更改, ...

  4. Centos根据系统VPS安装SendMail组件使WordPress支持E-mail

    1.在putty在链接: yum install sendmail 2.启动SendMail: service sendmail start 3.检查SendMail是否在监听默认的25port: n ...

  5. Flutter 基础组件:进度指示器

    前言 Material 组件库中提供了两种进度指示器:LinearProgressIndicator和CircularProgressIndicator,它们都可以同时用于精确的进度指示和模糊的进度指 ...

  6. 基于TDesign风格的Blazor企业级UI组件库

    作为一名Web开发人员,开发前端少不了使用JavaScript,而Blazor就是微软推出的基于.net平台交互式客户 Web UI 框架,可以使用C#替代JavaScript,减少我们的技术栈.降低 ...

  7. Vue2.0+组件库总结

    转自:https://blog.csdn.net/lishanleilixin/article/details/84025459 UI组件 element - 饿了么出品的Vue2的web UI工具套 ...

  8. 转:Vue2.0+组件库总结

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  9. Vue 2.0 组件库总结

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  10. 鸿蒙开源第三方件组件——轮播组件Banner

    目录: 1.功能展示 2.Sample解析 3.Library解析 4.<鸿蒙开源第三方组件>系列文章合集 前言 基于安卓平台的轮播组件Banner(https://github.com/ ...

随机推荐

  1. 【Python语法糖】闭包和装饰器

    Python闭包和装饰器 参考: https://zhuanlan.zhihu.com/p/453787908 https://www.bilibili.com/video/BV1JW411i7HR/ ...

  2. 把Customer Order的列表页面的代码,分离到组件里

    1.新增Shared文件夹,在Shared下新增OrdersListView.razor 2.在_Imports.razor文件里添加一行 3.重命名Pages/Trade目录下的OrdersList ...

  3. 【Azure Developer】如何通过Azure REST API 获取到虚拟机(VM)所使用的公共IP地址信息

    问题描述 如何通过Azure REST API 获取到虚拟机(VM)所使用的公共IP地址信息 问题解答 由于直接获取到的虚拟机信息(Virtual Machines - Get)中,并不会包含虚拟机的 ...

  4. C# 多线程(17):小总结

    前言 本篇内容是小总结和过渡,看完这篇后,就要开始继续学习 C# 多线程中的知识点啦~. 前面,经过 16 篇的学习,我们学习了多线程.锁.线程池.任务.同步.异步等知识,还没有使用到 async.a ...

  5. 代码随想录算法训练营第二十七天| 39. 组合总和 40.组合总和II 131.分割回文串

      39. 组合总和 卡哥建议:本题是 集合里元素可以用无数次,那么和组合问题的差别 其实仅在于 startIndex上的控制 题目链接/文章讲解:https://programmercarl.com ...

  6. RPA是啥?是干嘛的?如何入门开始使用?(一)

    1.RPA是啥? 我们先对RPA有一个大概的了解,再循序渐进. Robotic Process Automation(机器人流程自动化,简称RPA). 我的简单理解就是自动化,类似于按键精灵,相对来说 ...

  7. spring Cloud 有哪些组件?

    Eureka:服务注册与发现,每个服务都向eureka提供自己的ip地址,端口号,协议,通信协议,eureka将各个服务维护到一个服务清单中(双层map,第一层key为服务名,第二层key为实例名), ...

  8. liunx 设置默认python版本方法,

    Linux 中把Python3设为默认Python版本的几种方法 由于工作中要用到到python3.6  而服务器是2.7 ,这个低版本的2.7很多系统都要依赖,还不能删,同事建议建一个虚拟环境,但是 ...

  9. spring 注入参数时为list map写法用例

    导包基础:这了让服务器支持json 需要导入下面包 <dependency> <groupId>com.alibaba</groupId> <artifact ...

  10. Linux下编译成静态库和动态库,引入到项目中

    目录 配置全局变量 编译动态库和静态库: 动态库编译 静态库编译 AS mk 方式加载静态库和动态库 配置build.gradle 加载静态库方法 将libget.a考入到项目中 配置Android. ...