ohpm install @ohos/lottie
loadPullDownAnimation() { this.animateItem = lottie.loadAnimation({ container: this.context, // 渲染上下文 renderer: 'canvas', // canvas渲染模式 loop: 10, // 是否循环播放,默认true autoplay: true, // 是否自动播放,默认true name: this.animateName, contentMode: 'Contain', path: 'common/lottie/animation.json', // json路径 }) }
import lottie, { AnimationItem } from '@ohos/lottie' @Entry @Component struct RefreshExample { @State isRefreshing: boolean = false @State arr: String[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] private mainCanvasRenderingContext: CanvasRenderingContext2D = new CanvasRenderingContext2D() private animateItem: AnimationItem | null = null private animateName: string = 'pullDownAnimate' private setting: RenderingContextSettings = new RenderingContextSettings(true); private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.setting); private lottieName: string = 'lottie_data'; loadPullDownAnimation() { this.animateItem = lottie.loadAnimation({ container: this.context, // 渲染上下文 renderer: 'canvas', // canvas渲染模式 loop: 10, // 是否循环播放,默认true autoplay: true, // 是否自动播放,默认true name: this.animateName, contentMode: 'Contain', path: 'common/lottie/animation.json', // json路径 }) } @Builder customRefreshComponent() { Stack() { Row() { Canvas(this.context) .width('50%') .backgroundColor('#aabbcc') .onDisAppear(() => { lottie.destroy(this.lottieName) // Canvas销毁时顺带销毁lottie动画 }) .onReady(() => { if (this.mainCanvasRenderingContext) { this.mainCanvasRenderingContext.imageSmoothingEnabled = true; this.mainCanvasRenderingContext.imageSmoothingQuality = 'medium'; this.loadPullDownAnimation(); } else { console.error('mainCanvasRenderingContext is not initialized'); } }) } .alignItems(VerticalAlign.Center) } .align(Alignment.Center) .clip(true) // 设置最小高度约束保证自定义组件高度随刷新区域高度变化时自定义组件高度不会低于minHeight .constraintSize({ minHeight: 32 }) .width('100%') } build() { Column() { Refresh({ refreshing: $$this.isRefreshing, builder: this.customRefreshComponent() }) { List() { ForEach(this.arr, (item: string) => { ListItem() { Text('' + item) .width('70%') .height(80) .fontSize(16) .margin(10) .textAlign(TextAlign.Center) .borderRadius(10) .backgroundColor(0xFFFFFF) } }, (item: string) => item) } .onScrollIndex((first: number) => { console.info(first.toString()) }) .width('100%') .height('100%') .alignListItem(ListItemAlign.Center) .scrollBar(BarState.Off) } .backgroundColor(0x89CFF0) .pullToRefresh(true) .refreshOffset(64) .onStateChange((refreshStatus: RefreshStatus) => { console.info(`Refresh onStatueChange state is ${refreshStatus}`) if (refreshStatus === 0) { // 未下拉 this.animateItem!.destroy() this.animateItem = null } if (refreshStatus === 1) { // 下拉中 this.loadPullDownAnimation() } if (refreshStatus === 3) { // 刷新中 this.animateItem?.play(); } if (refreshStatus === 4) { // 刷新结束 setTimeout(() => { this.animateItem!.destroy() this.animateItem = null }, 75) } }) .onRefreshing(() => { setTimeout(() => { this.isRefreshing = false }, 2000) console.info(`onRefreshing test`) }) } } }
Q:在Tab栏中通过Refresh组件实现下拉刷新后,重复刷新同一项页面时,刷新效果不会重复显示。
A:可以给Refresh组件绑定一个参数用来控制刷新状态。
Refresh({ refreshing: $$this.isRefreshing })
每次重复刷新时,重置状态变量isRefreshing即可重复触发刷新效果。
Q:Refresh组件如何监听下拉高度变化?
A:可以通过onOffsetChange方法实时获取下拉高度,以此来控制下拉动画状态。