HarmonyOS 鸿蒙Next lottie重复加载失败
HarmonyOS 鸿蒙Next lottie重复加载失败 同时加载两个含有Lottie动画的弹窗,会发现第一个弹框没有动画,只有第二个有,这是啥情况?
可能是绑定了同一个canvas对象,可以看看下面代码
import lottie, { AnimationItem } from '@ohos/lottie';
import { ComponentContent } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct LottiePage {
canvas: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))
canvas2: CanvasRenderingContext2D = new CanvasRenderingContext2D(new RenderingContextSettings(true))
private animateItem1: AnimationItem | null = null;
private animateItem2: AnimationItem | null = null;
private animateName: string = "animationName1";
private animateName2: string = "animationName2";
build() {
Row() {
Button('openLottieDialog').onClick(() => {
let uiContext = this.getUIContext()
let promptAction = uiContext.getPromptAction();
let contentNode1 =
new ComponentContent(uiContext, wrapBuilder(buildLottie), new Params(this.canvas, this.loadAnimation));
let contentNode2 =
new ComponentContent(uiContext, wrapBuilder(buildLottie), new Params(this.canvas2, this.loadAnimation2));
try {
promptAction.openCustomDialog(contentNode1, {
isModal: false,
onDidDisappear: () => {
this.animateItem1!.removeEventListener("DOMLoaded");
lottie.destroy(this.animateName);
this.animateItem1 = null;
}
});
promptAction.openCustomDialog(contentNode2, {
isModal: false,
onDidDisappear: () => {
this.animateItem2!.removeEventListener("DOMLoaded");
lottie.destroy(this.animateName2);
this.animateItem2 = null;
}
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
};
})
}
}
loadAnimation = () => {
this.animateItem1 = lottie.loadAnimation({
container: this.canvas,
renderer: 'canvas', // canvas 渲染模式
loop: true,
autoplay: false,
name: this.animateName,
contentMode: 'Contain',
path: "common/animation.json",
})
this.animateItem1.addEventListener('DOMLoaded', (args: Object): void => {
this.animateItem1!.changeColor([225, 25, 100, 1]);
this.animateItem1!.play()
})
}
loadAnimation2 = () => {
this.animateItem2 = lottie.loadAnimation({
container: this.canvas2,
renderer: 'canvas', // canvas 渲染模式
loop: true,
autoplay: false,
name: this.animateName2,
contentMode: 'Contain',
path: "common/animation.json",
})
this.animateItem2.addEventListener('DOMLoaded', (args: Object): void => {
this.animateItem2!.changeColor([225, 25, 100, 1]);
this.animateItem2!.play()
})
}
}
@Builder
function buildLottie(param: Params) {
Column() {
// 关联画布
Canvas(param.canvas)
.width(200)
.height(200)
.backgroundColor(Color.Gray)
.onReady(() => {
param.loadAnimation();
})
}
.backgroundColor(Color.Pink)
.width(100)
.height(100)
.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
}
class Params {
canvas: CanvasRenderingContext2D
loadAnimation: () => void = () => {}
constructor(canvas: CanvasRenderingContext2D, loadAnimation: () => void) {
this.canvas = canvas;
this.loadAnimation = loadAnimation;
}
}
更多关于HarmonyOS 鸿蒙Next lottie重复加载失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
同一个页面,一起加载lottie动画是没问题的。建议排查播放动画日志,是不是弹框影响。
针对HarmonyOS(鸿蒙)系统中Lottie动画重复加载失败的问题,可能的原因及解决方法如下:
-
资源释放不彻底:在Lottie动画加载完成后,确保正确释放先前的动画资源。如果资源未彻底释放,可能导致后续加载时出现冲突或内存不足的情况。
-
动画文件损坏:检查Lottie动画文件是否完整无损。文件损坏可能导致加载失败或动画表现异常。
-
版本兼容性问题:确认使用的Lottie库版本与鸿蒙系统版本兼容。版本不兼容可能导致功能异常。
-
加载逻辑错误:检查动画加载的逻辑代码,确保在正确的时机和条件下加载动画,避免重复加载或加载时机不当导致的问题。
-
系统限制:了解鸿蒙系统对动画加载的具体限制和要求,确保动画加载符合系统规范。
-
日志分析:查看系统日志或应用日志,分析加载失败时的错误信息,以便定位问题原因。
如果上述方法均无法解决问题,可能是系统深层次的bug或特定环境下的特殊问题。此时,建议联系鸿蒙系统的官方技术支持团队进行深入分析。如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html 。