Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate image sequence on React Native

Tags:

react-native

I have an animation consisting of a sequence of images: image01.png, image02.png, image03.png, etc. How do I get these to animate continuously on React Native?

like image 758
Tarek Avatar asked Jul 26 '16 03:07

Tarek


People also ask

How do you loop animation in React Native?

In order to create looping animations in react native, we should use Animated. loop() method. Wrap your Animations within Animated. loop() and the animations will act as a loop.

What is interpolate React Native?

Interpolation is a way of estimating a function at intermediate points, learning from the ranges you provide. In React Native, you can interpolate animated values using . interpolate which takes an inputRange, that interpolates and maps the values to an outputRange.


1 Answers

You can try with libraries:

  • https://github.com/madsleejensen/react-native-image-sequence
  • https://github.com/remobile/react-native-image-animation

First is more more efficient, second is in pure javascript. Another way is to implement it on your own way, like here: https://github.com/facebook/react-native/issues/9280

It should looks like this

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}
            />
        )
    }
}
like image 121
jonzee Avatar answered Oct 17 '22 11:10

jonzee