在React Native上动画图像序列

Tar*_*rek 7 react-native

我有一个动画组成的图像序列的:image01.png,image02.png,image03.png等我如何获得这些不断在动画阵营本土?

jon*_*zee 9

您可以尝试使用库:

首先是更高效,第二是纯JavaScript.另一种方法是按照自己的方式实现它,如:https://github.com/facebook/react-native/issues/9280

它看起来应该是这样的

export default class Animation extends Component {
    constructor(props) {
        super(props);
        this.images = [
            require('./img_01.png'),
            require('./img_02.png'),
            require('./img_03.png'),
        ];
        this.next = this.next.bind(this);
        this.state = {index: 0};
    }

    componentDidMount() {
        this.next();
    }

    next() {
        setTimeout(() => {
            this.setState({index: (this.state.index+1)%3});
            this.next();
        }, 300);
    }

    render() {
        return (
            <Image
              source={this.images[this.state.index]}
              style={styles.image}
            />
        )
    }
}
Run Code Online (Sandbox Code Playgroud)