loop设置false ,每次点击调play,第二次再点击 调用play 就不执行动画了,怎么解决?

loop设置false ,每次点击调play,第二次再点击 调用play 就不执行动画了,怎么解决?只能每次点击都要销毁前一个动画重新创建动画吗。(就是实现带有lottie的底部导航栏遇到的问题)


HarmonyOS
2024-11-15 11:42:20
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

下面是一个参考demo。

import lottie from "@ohos/lottie"; 
@Entry 
@Component 
struct Index { 
  @State currentIndex: number = 0; 
  private renderingSettings1: RenderingContextSettings = new RenderingContextSettings(true); 
  private canvasRenderingContext1: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.renderingSettings1); 
  private renderingSettings2: RenderingContextSettings = new RenderingContextSettings(true); 
  private canvasRenderingContext2: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.renderingSettings2); 
  private renderingSettings3: RenderingContextSettings = new RenderingContextSettings(true); 
  private canvasRenderingContext3: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.renderingSettings3); 
  private tabBarOption1: TabBarOption = { 
    index: 0, 
    text: 'tabbar1', 
    name: 'tab1', 
    path: "common/lottie/tabbar2.json", 
    canvasRenderingContext: this.canvasRenderingContext1 
  } 
  private tabBarOption2: TabBarOption = { 
    index: 1, 
    text: 'tabbar2', 
    name: 'tab2', 
    path: "common/lottie/tabbar3.json", 
    canvasRenderingContext: this.canvasRenderingContext2 
  } 
  private tabBarOption3: TabBarOption = { 
    index: 2, 
    text: 'tabbar3', 
    name: 'tab3', 
    path: "common/lottie/tabbar4.json", 
    canvasRenderingContext: this.canvasRenderingContext3 
  } 
  private data1: string[] = ['我的评论', '与我相关', '个人中心1', '个人中心2', '个人中心3', '我的发布', '设置', '退出登录']; 
  private data2: string[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']; 
  private data3: string[] = ['我的评论', '与我相关', '个人中心1', '个人中心2', '个人中心3', '我的发布', '设置', '退出登录']; 
  @Builder 
  TabBar(tabBarOption: TabBarOption) { 
    Column() { 
      Canvas(tabBarOption.canvasRenderingContext) 
        .width(60) 
        .height(60) 
        .backgroundColor(Color.White) 
        .onReady(() => { 
          lottie.loadAnimation({ 
            container: tabBarOption.canvasRenderingContext, 
            renderer: 'canvas', 
            loop: false, 
            autoplay: false, 
            name: tabBarOption.name, 
            path: tabBarOption.path, 
          }) 
          this.lottieController(); 
        }) 
      Text(tabBarOption.text) 
        .fontSize(18) 
        .fontColor(this.currentIndex === tabBarOption.index ? '#03c067' : '#999') 
    } 
    .height(100) 
  } 
  build() { 
    Tabs({ barPosition: BarPosition.End }) { 
      TabContent() { 
        List({ space: 20 }) { 
          ForEach(this.data1, (item: string) => { 
            ListItem() { 
              Text(item) 
                .width('100%') 
                .height(150) 
                .fontSize(20) 
                .textAlign(TextAlign.Center) 
                .backgroundColor('#95efd2') 
            } 
          }) 
        } 
        .backgroundColor('#eeeeee') 
        .divider({ strokeWidth: 1, color: 0x222222 }) 
        .edgeEffect(EdgeEffect.None) 
      }.tabBar(this.TabBar(this.tabBarOption1)) 
      TabContent() { 
        List({ space: 20 }) { 
          ForEach(this.data2, (item: string) => { 
            ListItem() { 
              Text(item) 
                .width('100%') 
                .height(150) 
                .fontSize(20) 
                .textAlign(TextAlign.Center) 
                .backgroundColor('#95efd2') 
            } 
          }) 
        } 
        .backgroundColor('#eeeeee') 
        .divider({ strokeWidth: 1, color: 0x222222 }) 
        .edgeEffect(EdgeEffect.None) 
      }.tabBar(this.TabBar(this.tabBarOption2)) 
      TabContent() { 
        List({ space: 20 }) { 
          ForEach(this.data3, (item: string) => { 
            ListItem() { 
              Text(item) 
                .width('100%') 
                .height(150) 
                .fontSize(20) 
                .textAlign(TextAlign.Center) 
                .backgroundColor('#95efd2') 
            } 
          }) 
        } 
        .backgroundColor('#eeeeee') 
        .divider({ strokeWidth: 1, color: 0x222222 }) 
        .edgeEffect(EdgeEffect.None) 
      }.tabBar(this.TabBar(this.tabBarOption3)) 
    } 
    .barHeight(100) 
    .onChange((index: number) => { 
      this.currentIndex = index; 
      this.lottieController(); 
    }) 
  } 
  lottieController() { 
    if (this.currentIndex == 0) { 
      lottie.stop(); 
      lottie.play(this.tabBarOption1.name); 
    } 
    if (this.currentIndex == 1) { 
      lottie.stop(); 
      lottie.play(this.tabBarOption2.name); 
    } 
    if (this.currentIndex == 2) { 
      lottie.stop(); 
      lottie.play(this.tabBarOption3.name); 
    } 
  } 
} 
 
interface TabBarOption { 
  index: number; 
  text: string; 
  name: string; 
  path: string; 
  canvasRenderingContext: CanvasRenderingContext2D; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
分享
微博
QQ
微信
回复
2024-11-15 16:48:09
相关问题
HarmonyOS aioxs二次封装
1009浏览 • 1回复 待解决
HarmonyOS 权限二次申请
867浏览 • 1回复 待解决
HarmonyOS getStringByName方法二次封装
1015浏览 • 1回复 待解决
class二次刷新渲染数组
1560浏览 • 1回复 待解决
实现二次侧滑退出应用
2799浏览 • 1回复 待解决
HarmonyOS cocos引擎能否二次启动
1221浏览 • 1回复 待解决
华为账号实时验证/二次放号相关咨询
2881浏览 • 1回复 待解决
华为荣耀play3可以升级鸿蒙吗
13131浏览 • 1回复 待解决
老荣耀啥时候更新鸿蒙啊,荣耀play?
5523浏览 • 1回复 待解决
荣耀play能升级鸿蒙OS2.0嘛?
6696浏览 • 1回复 待解决