简体   繁体   English

React Native 如何使用 Lottie JSON 文件?

[英]React Native How to use Lottie JSON file?

Hey guys I want to use Lottie with React Native to show a glas of water that gets filled up.大家好,我想用 Lottie 和 React Native 来展示一杯装满的水。

I found some ready Lottie files that look pretty good and I wanted to use them inside my app.我发现了一些看起来不错的现成 Lottie 文件,我想在我的应用程序中使用它们。

I installed Lottie and linked it (with rn-link and I tried it manually..).我安装了 Lottie 并链接了它(使用 rn-link 并且我手动尝试了它..)。

I had an Error showing that stated TypeError: Cannot read property "Commands" of undefined.我有一个错误,显示 TypeError:无法读取未定义的属性“命令”。 - but after changing the source it disapeared. - 但是在更改来源后它消失了。 The problem I have now is that it is only showing me a white screen and nothing is happening.我现在遇到的问题是它只向我显示一个白屏并且什么都没有发生。

I am even using the official code that is taken from Lottie Docs ( https://airbnb.io/lottie/#/react-native )我什至在使用取自 Lottie Docs 的官方代码( https://airbnb.io/lottie/#/react-native

I tried it with 2 different animations (1: https://lottiefiles.com/15421-glass-of-water , 2: https://lottiefiles.com/5922-water-loading?lang=de ) - they are in my assets folder and inside the android/assets folder also!我尝试了 2 种不同的动画(1: https://lottiefiles.com/15421-glass-of-water,2https://lottiefiles.com/5922-water-loading?lang=de ) - 它们在我的资产文件夹和 android/assets 文件夹内!

Here is the code:这是代码:

    import React from 'react';
    import { Animated, Easing } from 'react-native';
    import LottieView from 'lottie-react-native';

import Lottiewater from "../assets/Lottiewater.json";
import LottieGlas from "../assets/5922-water-loading.json"

    export default class Water extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          progress: new Animated.Value(0),
        };
      }

      componentDidMount() {
        Animated.timing(this.state.progress, {
          toValue: 1,
          duration: 5000,
          easing: Easing.linear,
          useNativeDriver: true
        }).start();
      }

      render() {
        return (
          <LottieView source={require('../assets/Lottiewater.json')} progress={this.state.progress} />
          <LottieView source={LottieGlas} progress={this.state.progress} autoSize />
        );
      }
    }


    // OR the code below! 



import Lottiewater from "../assets/Lottiewater.json";
import LottieGlas from "../assets/5922-water-loading.json"

import React from 'react';
import LottieView from 'lottie-react-native';

export default class Water extends React.Component {
  componentDidMount() {
    this.animation.play();
    // Or set a specific startFrame and endFrame with:
    // this.animation.play(30, 120);
  }

  render() {
    return (
      <LottieView
        ref={animation => {
          this.animation = animation;
        }}
            // source={require('../assets/Lottiewater.json')}
            source={Lottiewater}
          />
        );
      }
    }

Okay so I fixxed it - for me the issue was that I did not build the app again.好的,所以我修复了它 - 对我来说,问题是我没有再次构建应用程序。 So closing the emulator and restarting Android Studio to rebuild fixxed it for me.所以关闭模拟器并重新启动 Android Studio 来重建它为我修复了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM