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 = 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應用組件化:
最后將TriangularIndicator暴露出來供外部引用:struct TriangularIndicator {
//獲取TriangularIndicator實例
null) model: TriangularIndicator.Model = new TriangularIndicator.Model(
//初始化指示器當前index
itemIndex: number
//設置指示器總條數
0 count: number =
//再分別實現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()
new TriangularIndicator.Model(this.controller) model: TriangularIndicator.Model =
number=0 itemIndex:
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文章
669瀏覽量
30219 -
應用程序
+關注
關注
38文章
3324瀏覽量
58839
原文標題:CircleIndicator組件,使指示器風格更加多樣化
文章出處:【微信號:gh_e4f28cfa3159,微信公眾號:OpenAtom OpenHarmony】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
RTsmart源碼編譯錯誤,提醒我缺少文件導致make失敗,為什么?
【經驗分享】在Omni3576上編譯Redis-8.0.2源碼,并安裝及性能測試

2K0300 OpenHarmony源碼構建指南
SSM框架的源碼解析與理解
需要做一個用ADS1282EVM-PDK實現信號數據采集功能的課題,請問有源碼么?
基于無操作系統的STM32單片機開發附源碼
ElfBoard技術貼|在NXP源碼基礎上適配ELF 1開發板的按鍵功能

評論