zhangbo1_wd

列表页面lottie动画crash问题修复。下拉刷新动画使用优化

@@ -85,8 +85,8 @@ export default struct CustomLayout { @@ -85,8 +85,8 @@ export default struct CustomLayout {
85 name: this.animateName, 85 name: this.animateName,
86 path: "lottie/refresh_step1.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径 86 path: "lottie/refresh_step1.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径
87 }) 87 })
88 - }  
89 this.animateItem.goToAndStop(1) 88 this.animateItem.goToAndStop(1)
  89 + }
90 let total = CustomLayout.REFRESH_HEIGHT 90 let total = CustomLayout.REFRESH_HEIGHT
91 let progress = offset * 100 / total 91 let progress = offset * 100 / total
92 this.animateItem?.goToAndStop(this.getFramesByProgress(progress), true); 92 this.animateItem?.goToAndStop(this.getFramesByProgress(progress), true);
@@ -103,9 +103,6 @@ export default struct CustomLayout { @@ -103,9 +103,6 @@ export default struct CustomLayout {
103 path: "lottie/refresh_step2.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径 103 path: "lottie/refresh_step2.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径
104 }) 104 })
105 } 105 }
106 - // this.animateItem2.isLoaded  
107 - // TODO 是否拦截重复触发  
108 - this.animateItem2.goToAndPlay(1)  
109 } 106 }
110 107
111 getFramesByProgress(progress: number): number { 108 getFramesByProgress(progress: number): number {
@@ -18,14 +18,18 @@ export struct CustomPullToRefresh { @@ -18,14 +18,18 @@ export struct CustomPullToRefresh {
18 .setAnimDuration(500); 18 .setAnimDuration(500);
19 private refreshSettings: RenderingContextSettings = new RenderingContextSettings(true) 19 private refreshSettings: RenderingContextSettings = new RenderingContextSettings(true)
20 private refreshContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.refreshSettings) 20 private refreshContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.refreshSettings)
  21 + private refreshingSettings: RenderingContextSettings = new RenderingContextSettings(true)
  22 + private refreshingContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.refreshingSettings)
21 private loadMoreSettings: RenderingContextSettings = new RenderingContextSettings(true) 23 private loadMoreSettings: RenderingContextSettings = new RenderingContextSettings(true)
22 private loadMoreContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.loadMoreSettings) 24 private loadMoreContext: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.loadMoreSettings)
23 private refreshAnimation: AnimationItem | null = null; 25 private refreshAnimation: AnimationItem | null = null;
  26 + private refreshingAnimation: AnimationItem | null = null;
24 private loadMoreAnimation: AnimationItem | null = null; 27 private loadMoreAnimation: AnimationItem | null = null;
25 private refreshAnimName: string = "refresh"; 28 private refreshAnimName: string = "refresh";
26 private refreshingAnimName: string = "refreshing"; 29 private refreshingAnimName: string = "refreshing";
27 private loadMoreAnimName: string = "loadMore"; 30 private loadMoreAnimName: string = "loadMore";
28 private refreshAnimationDestroy = true 31 private refreshAnimationDestroy = true
  32 + private refreshingAnimationDestroy = true
29 // refresh-1,refreshing-2,refreshed-3,idle-4 33 // refresh-1,refreshing-2,refreshed-3,idle-4
30 @State @Watch('stateChange') private refreshState: number = 4; 34 @State @Watch('stateChange') private refreshState: number = 4;
31 35
@@ -120,9 +124,23 @@ export struct CustomPullToRefresh { @@ -120,9 +124,23 @@ export struct CustomPullToRefresh {
120 }) 124 })
121 .onDisAppear(() => { 125 .onDisAppear(() => {
122 lottie.destroy(this.refreshAnimName); 126 lottie.destroy(this.refreshAnimName);
  127 + })
  128 + .visibility(this.refreshState == 1 ? Visibility.Visible : Visibility.Hidden)
  129 +
  130 + Canvas(this.refreshingContext)
  131 + .width(60)
  132 + .height(60)
  133 + .backgroundColor(Color.Transparent)
  134 + .onReady(() => {
  135 + // 可在此生命回调周期中加载动画,可以保证动画尺寸正确
  136 + //抗锯齿的设置
  137 + this.refreshContext.imageSmoothingEnabled = true;
  138 + this.refreshContext.imageSmoothingQuality = 'medium'
  139 + })
  140 + .onDisAppear(() => {
123 lottie.destroy(this.refreshingAnimName); 141 lottie.destroy(this.refreshingAnimName);
124 }) 142 })
125 - .visibility((this.refreshState == 1 || this.refreshState == 2) ? Visibility.Visible : Visibility.Hidden) 143 + .visibility(this.refreshState == 2 ? Visibility.Visible : Visibility.Hidden)
126 144
127 Text('已更新至最新') 145 Text('已更新至最新')
128 .fontSize(14) 146 .fontSize(14)
@@ -175,10 +193,7 @@ export struct CustomPullToRefresh { @@ -175,10 +193,7 @@ export struct CustomPullToRefresh {
175 } 193 }
176 194
177 private refreshAnim(percent: number) { 195 private refreshAnim(percent: number) {
178 - if (this.refreshAnimation == null || this.refreshAnimation.name != this.refreshAnimName ||  
179 - this.refreshAnimationDestroy) {  
180 - this.refreshAnimation?.destroy(this.refreshAnimName)  
181 - this.refreshAnimation?.destroy(this.refreshingAnimName) 196 + if (this.refreshAnimation == null || this.refreshAnimationDestroy) {
182 this.refreshAnimation = lottie.loadAnimation({ 197 this.refreshAnimation = lottie.loadAnimation({
183 container: this.refreshContext, 198 container: this.refreshContext,
184 renderer: 'canvas', // canvas 渲染模式 199 renderer: 'canvas', // canvas 渲染模式
@@ -212,21 +227,19 @@ export struct CustomPullToRefresh { @@ -212,21 +227,19 @@ export struct CustomPullToRefresh {
212 } 227 }
213 228
214 private refreshingAnim() { 229 private refreshingAnim() {
215 - // Logger.error('zzzz', 'animate2, 1')  
216 // 先销毁之前的动画 230 // 先销毁之前的动画
217 - if (this.refreshAnimation == null || this.refreshAnimation.name != this.refreshingAnimName ||  
218 - this.refreshAnimationDestroy) {  
219 - this.refreshAnimation?.destroy(this.refreshAnimName)  
220 - this.refreshAnimation?.destroy(this.refreshingAnimName)  
221 - this.refreshAnimation = lottie.loadAnimation({  
222 - container: this.refreshContext, 231 + if (this.refreshingAnimation == null || this.refreshingAnimationDestroy) {
  232 + this.refreshingAnimation?.destroy(this.refreshingAnimName)
  233 + this.refreshingAnimation = lottie.loadAnimation({
  234 + container: this.refreshingContext,
223 renderer: 'canvas', // canvas 渲染模式 235 renderer: 'canvas', // canvas 渲染模式
224 loop: 10, 236 loop: 10,
225 autoplay: true, 237 autoplay: true,
226 name: this.refreshingAnimName, 238 name: this.refreshingAnimName,
227 path: "lottie/refresh_step2.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径 239 path: "lottie/refresh_step2.json", // 路径加载动画只支持entry/src/main/ets 文件夹下的相对路径
228 }) 240 })
229 - this.addRefreshAnimListener() 241 + this.refreshingAnimationDestroy = false
  242 + this.addRefreshingAnimListener()
230 } 243 }
231 // Logger.error('zzzz', 'animate2, 2') 244 // Logger.error('zzzz', 'animate2, 2')
232 } 245 }
@@ -242,6 +255,13 @@ export struct CustomPullToRefresh { @@ -242,6 +255,13 @@ export struct CustomPullToRefresh {
242 }); 255 });
243 } 256 }
244 257
  258 + private addRefreshingAnimListener() {
  259 + this.refreshingAnimation?.addEventListener('destroy', (args: Object): void => {
  260 + // Logger.error('zzzz', "lottie destroy");
  261 + this.refreshingAnimationDestroy = true
  262 + });
  263 + }
  264 +
245 loadMoreAnimate() { 265 loadMoreAnimate() {
246 if (this.loadMoreAnimation == null) { 266 if (this.loadMoreAnimation == null) {
247 this.loadMoreAnimation = lottie.loadAnimation({ 267 this.loadMoreAnimation = lottie.loadAnimation({