UI界面是應用程序可視化必不可少的部分。設計精致的UI界面可以使得整個可視化應用程序給用戶留下深刻的印象,是改善用戶界面體驗最直接的方式。
ArkUI開發框架為開發者提供了豐富的UI原生組件,如Navigation(Page頁面的根容器)、ScrollBar(滾動條組件)、Swiper(滑動容器)及Tabs(通過頁簽進行內容視圖切換的容器組件)等。其中,Swiper組件和Tabs組件在應用程序開發中對于指示器的使用引入較多,但是直接使用原生的Swiper組件和Tabs組件不適用于表現復雜的UI指示器交互變化。因此,我們針對Swiper組件和Tabs組件在指示器應用方向做了一個簡單的封裝,以CiecleIndicator三方組件的形式提供應用程序依賴使用,從而提升了ArkUI開發框架的UI界面之指示器風格多樣化的能力。
CircleIndicator介紹
CircleIndicator組件UI效果展示
圓形指示器:













什么是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 = heightreturn this}//觸摸事件攔截onPageTouch: (event: TouchEvent, currentIndicator: number) => voidpublic notifyTouch(event: TouchEvent, currentIndex: number) {this.onPageTouch(event, currentIndex)}//設置構造器private tabsController: TabsController(tabsController: TabsController) {this.tabsController = tabsController}//頁面切換監聽indexChange: (itemIndex: number) => voidpublic setChangeListener(callback: (index: number) => void): Model{this.indexChange = callbackreturn this}}
將TriangularIndicator應用組件化:
最后將TriangularIndicator暴露出來供外部引用:struct TriangularIndicator {//獲取TriangularIndicator實例model: TriangularIndicator.Model = new TriangularIndicator.Model(null)//初始化指示器當前indexitemIndex: number//設置指示器總條數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)}}
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類型成員以記錄當前頁下標
2.屬性設置:通過Model類對象設置UI屬性來自定義所需風格,也可以添加所需的回調private controller: TabsController = new TabsController()model: TriangularIndicator.Model = new TriangularIndicator.Model(this.controller)itemIndex:number=0
3.界面繪制:在Tabs組件旁放置Indicator組件,注意需要關閉原生的bar。并監聽Tabs組件的touch事件,通知給Model類,以統一處理滑動邏輯aboutToAppear() {this.model.setReverse(true).setLineHeight(4).setTriangleHeight(10).setLineColor("#e94220").setBackgroundColor("#eeeeee").setChangeListener((itemIndex) => {console.info("change page to " + this.data[itemIndex])})}
本期基于OpenHarmony API8的CircleIndicator組件1.0.3(https://gitee.com/openharmony-sig/CircleIndicator/tree/1.0.3)就為大家介紹到這,歡迎大家積極參與貢獻。了解更多三方組件動態,請關注三方組件資源匯總(https://gitee.com/openharmony-tpc/tpc_resource),更多優秀的組件等你來發現!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")}
-
源碼
+關注
關注
8文章
685瀏覽量
31323 -
應用程序
+關注
關注
38文章
3344瀏覽量
60262
原文標題:CircleIndicator組件,使指示器風格更加多樣化
文章出處:【微信號:gh_e4f28cfa3159,微信公眾號:OpenAtom OpenHarmony】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
SaaS云門診系統源碼,診所his系統源碼
一款基于Java+Spring Boot+Vue的智慧隨訪管理系統源碼
醫院隨訪管理系統源碼,三級隨訪系統源碼,Java+Springboot,Vue,Ant-Design+MySQL5
E203工程源碼時鐘樹解析
揭秘Air780EGH定位功能:源碼開放助力極速開發 !
RTsmart源碼編譯錯誤,提醒我缺少文件導致make失敗,為什么?
【經驗分享】在Omni3576上編譯Redis-8.0.2源碼,并安裝及性能測試
【每周推薦】基于STM32開發項目實例下載(含PCB、原理圖、源碼等)
什么是CircleIndicator?CircleIndicator的源碼實現
評論