Facebook’s react-native platform has an animation API that lets you animate Text, Image or View components. You can animate properties of the components, then make them play on an event, chain them together sequentially, or play them all together.
Styles you can animate:
Here are some stylesheet properties of components that you can animate:
- Opacity
- Width
- Height
- Translation offsets
- Rotation
Animation variables:
One-dimensional values to be animated (such as opacity or height) are stored in an Animated.Value
object, while two-dimensional values (such as XY translation) are stored in an Animated.ValueXY
object. These are initialized like this:
import { 'Animated' } from react-native; this.state = { opacityValue: new Animated.Value(255), translation: new Animated.ValueXY(0, 0) };
Specifying animation types
There are three ways to procedurally change the value of an animation variable: spring
, decay
, and toValue
. To call these functions, you must specify which variable is being configured, and a configuration object, with parameters described below. In addition, the target value at the end of the animation is called toValue
.
Spring
is straightforward to use; it causes the value to bounce, and you can optionally set the friction (“bounciness”) and tension (“speed”).Decay
is also straightforward to use; it’s simply an exponential decay function where you set the rate of decay (e.g., 0.97) and the initial velocity, which is required.timing
has 3 parameters that you can set: delay and duration are obvious, but the third one, easing, requires a deeper understanding to use. The default easing function is linear, but if you want to override it to something like Easing.sin(t) or Easing.bezier(x1, y1, x2, y2), you must add/integrate the line “import { ‘Easing’ } from ‘react-native'” to the top of your source code. The easing functions you can use are here.
Example to get something to fade out over 200ms:
Animated.timing(this.state.opacity, {toValue: 0, duration: 200})
You can also set the value directly simply by calling Animated.setValue()
.
Assigning animation variables to a component
Using the above example of opacity, just set a stylesheet property:
render() { let myStyle = { opacity: this.state.opacityValue, }; return ( <Animated.View style={myStyle}> <Text>Woooo!</Text> </Animated.View> ); }
Note how <Animated.View>
is used instead of <View>
. For Animated.ValueXY
variables, you may have to get the X and Y values directly by accessing myXYValue.x._value
and myXYValue.y._value
.
Calling the animation
To start an animation, you simply call .start()
on the Animated object, optionally passing a callback function.
Animated.timing(this.state.opacity, {toValue: 0, duration: 200}).start(() => Alert.alert('Animation done!'))
The callback function is how you get animations to loop. At the time of writing, react-native does not have built-in parameters for looping an animation.
Sequential and parallel animations
You can start animations in sequence or in parallel by calling Animated.sequence()
or Animated.parallel()
with an array of Animation calls. It’s better explained by an example:
Animated.parallel([ Animated.spring(this.state.heartSize, { toValue: 1, friction: 0.7, tension: 0.4 }), Animated.spring(this.state.circleSize, { toValue: 0.5, friction: 1.0, tension: 0.3 }), ]).start();
Summary
- Create Animation variables by using
Animated.Value()
- Assign Animation variables to stylesheet properties attached to
<Animated.View>
,<Animated.Text>
or<Animated.Image>
tags - Configure an animation by calling
Animated.timing()
,Animated.spring()
, orAnimated.decay()
- Start an animation by calling
.start()
on the configured animation - Start animations in sequence or parallel by wrapping multiple animations in an array and passing the array as a parameter to
Animation.sequence()
orAnimation.parallel()
- For more details, see the Animated API reference or the react-native Animations guide
Full code example
import React, { Component } from 'react'; import { Alert, Animated, Button, Text, View } from 'react-native'; export default class AnimationView extends Component { constructor(props) { super(props); this.state = { opacityValue: new Animated.Value(1), heartSize: new Animated.Value(0.5), circleSize: new Animated.Value(1.0), }; } render() { let textStyle = { opacity: this.state.opacityValue }; let heartStyle = { transform: [{scale: this.state.heartSize}] } let circleStyle = { transform: [{scale: this.state.circleSize}] } return ( <View> <View> <Button onPress={() => { Animated.timing(this.state.opacityValue, { toValue: 0, duration: 1000 }).start(() => Alert.alert('Animation done!')); }} title="Fade text away" color="powderblue" /> <View style={{backgroundColor: "powderblue"}}> <Animated.Text style={textStyle}>Fade away</Animated.Text> </View> </View> <View> <Button onPress={() => { Animated.parallel([ Animated.spring(this.state.heartSize, { toValue: 1, friction: 0.7, tension: 0.4 }), Animated.spring(this.state.circleSize, { toValue: 0.5, friction: 1.0, tension: 0.3 }), ]).start(); }} title="Initiate love" color="pink" /> <View style={{flexDirection: "row", backgroundColor: "white"}}> <Animated.Image style={heartStyle} source={require('./heart.png')}/> <Animated.Image style={circleStyle} source={require('./circle.png')}/> </View> </View> </View> ) } }
Zho8OcGzhr8
YExkRpgzj5Z
Very good https://is.gd/N1ikS2
Awesome https://shorturl.fm/oYjg5
https://shorturl.fm/YvSxU
https://shorturl.fm/m8ueY
https://shorturl.fm/XIZGD
https://shorturl.fm/j3kEj
https://shorturl.fm/5JO3e
https://shorturl.fm/A5ni8
https://shorturl.fm/A5ni8
https://shorturl.fm/j3kEj
https://shorturl.fm/XIZGD
https://shorturl.fm/YvSxU
https://shorturl.fm/a0B2m
https://shorturl.fm/FIJkD
https://shorturl.fm/XIZGD
https://shorturl.fm/XIZGD
https://shorturl.fm/TbTre
https://shorturl.fm/6539m
https://shorturl.fm/A5ni8
https://shorturl.fm/47rLb
https://shorturl.fm/IPXDm
https://shorturl.fm/ypgnt
https://shorturl.fm/hevfE
https://shorturl.fm/f4TEQ
https://shorturl.fm/nqe5E
https://shorturl.fm/Kp34g
Apply now and unlock exclusive affiliate rewards! https://shorturl.fm/DK3fr
Drive sales, collect commissions—join our affiliate team! https://shorturl.fm/Mpk0q
Start earning passive income—become our affiliate partner! https://shorturl.fm/sB7AJ
Sign up and turn your connections into cash—join our affiliate program! https://shorturl.fm/qBY3Q
Partner with us and earn recurring commissions—join the affiliate program! https://shorturl.fm/iOQVH
Refer and earn up to 50% commission—join now! https://shorturl.fm/Yod0w
Join forces with us and profit from every click! https://shorturl.fm/mbcln
Become our affiliate and watch your wallet grow—apply now! https://shorturl.fm/LMnk3
Start earning passive income—join our affiliate network today! https://shorturl.fm/9L970
Promote our products—get paid for every sale you generate! https://shorturl.fm/ELoRp
Start sharing our link and start earning today! https://shorturl.fm/BN3p4
Start sharing, start earning—become our affiliate today! https://shorturl.fm/GiGaq
Promote our products and earn real money—apply today! https://shorturl.fm/i7e8w
Partner with us and enjoy recurring commission payouts! https://shorturl.fm/PefZe
Turn referrals into revenue—sign up for our affiliate program today! https://shorturl.fm/FFfdG
Share your link and rake in rewards—join our affiliate team! https://shorturl.fm/BaW5c
Drive sales, earn commissions—apply now! https://shorturl.fm/w9Rln
Promote, refer, earn—join our affiliate program now! https://shorturl.fm/hpEpg
Join our affiliate family and watch your profits soar—sign up today! https://shorturl.fm/naDtV
Refer friends and colleagues—get paid for every signup! https://shorturl.fm/sEf5A
Share your unique link and cash in—join now! https://shorturl.fm/dSruV
Share our products, reap the rewards—apply to our affiliate program! https://shorturl.fm/PtjAV
Your influence, your income—join our affiliate network today! https://shorturl.fm/EqMNk
Refer friends, earn cash—sign up now! https://shorturl.fm/Roh6X
Promote our brand, reap the rewards—apply to our affiliate program today! https://shorturl.fm/AiQn2
Maximize your income with our high-converting offers—join as an affiliate! https://shorturl.fm/qvHn0
Partner with us for high-paying affiliate deals—join now! https://shorturl.fm/tVWhp
Turn your traffic into cash—join our affiliate program! https://shorturl.fm/fs9ei
Join our affiliate community and start earning instantly! https://shorturl.fm/t0lFK
Become our affiliate and watch your wallet grow—apply now! https://shorturl.fm/9E79l
Sign up for our affiliate program and watch your earnings grow! https://shorturl.fm/groc8
Start profiting from your traffic—sign up today! https://shorturl.fm/zA8On
Be rewarded for every click—join our affiliate program today! https://shorturl.fm/JhFz7
Sign up for our affiliate program and watch your earnings grow! https://shorturl.fm/8tJlk
Start profiting from your network—sign up today! https://shorturl.fm/FHBXs
Get started instantly—earn on every referral you make! https://shorturl.fm/uEDrj
Be rewarded for every click—join our affiliate program today! https://shorturl.fm/HcFCw
Refer customers, collect commissions—join our affiliate program! https://shorturl.fm/laiit
Promote our brand and watch your income grow—join today! https://shorturl.fm/eunYI
Promote our products—get paid for every sale you generate! https://shorturl.fm/LZXyy
Become our partner now and start turning referrals into revenue! https://shorturl.fm/zNe1c
Monetize your traffic instantly—enroll in our affiliate network! https://shorturl.fm/GHwSp
Share your unique link and earn up to 40% commission! https://shorturl.fm/l7Jii
Get started instantly—earn on every referral you make! https://shorturl.fm/pD7kQ
https://shorturl.fm/AOmlN
https://shorturl.fm/Q2FY3
https://shorturl.fm/175m4
https://shorturl.fm/PzSIr
https://shorturl.fm/Ea40l
https://shorturl.fm/cF3QH
https://shorturl.fm/SaASH
https://shorturl.fm/kPolq
https://shorturl.fm/jCfgU
https://shorturl.fm/y7mp4
https://shorturl.fm/KWoxi
https://shorturl.fm/8XsRx
https://shorturl.fm/Gdpk6
https://shorturl.fm/XfaVK
https://shorturl.fm/WpvLb
https://shorturl.fm/DtuEA
https://shorturl.fm/p40xz
https://shorturl.fm/84uQB
https://shorturl.fm/w0xOx
https://shorturl.fm/kgu1a
https://shorturl.fm/2ENpX
https://shorturl.fm/adMqH
https://shorturl.fm/XJMXy
https://shorturl.fm/kmtCF
https://shorturl.fm/M15Jn
https://shorturl.fm/AWB6U
https://shorturl.fm/XjJdn
https://shorturl.fm/2NFzA
https://shorturl.fm/yhxOF
https://shorturl.fm/yhsOM
https://shorturl.fm/0V3T6
https://shorturl.fm/Dw856
https://shorturl.fm/gQ3Kg
https://shorturl.fm/66i4F
https://shorturl.fm/qnqRd
https://shorturl.fm/I215V
https://shorturl.fm/scswK
https://shorturl.fm/LWJPj
https://shorturl.fm/sV0K9
https://shorturl.fm/LhH11
https://shorturl.fm/n2uF4
https://shorturl.fm/TKKX7
https://shorturl.fm/cwsOS
https://shorturl.fm/d0mMe
https://shorturl.fm/ZOujd
https://shorturl.fm/vpqgZ
https://shorturl.fm/TFpfl
https://shorturl.fm/Cl57y
https://shorturl.fm/bEXCN
https://shorturl.fm/5NBs5
https://shorturl.fm/nkOgt
https://shorturl.fm/B94Nx
https://shorturl.fm/GR2qO
https://shorturl.fm/kRR5q
https://shorturl.fm/5sTII
https://shorturl.fm/IWagG
https://shorturl.fm/k4pQu
https://shorturl.fm/k3wSr
https://shorturl.fm/Hu66W
https://shorturl.fm/XM8mL
https://shorturl.fm/pOiK7
https://shorturl.fm/qSiNk
https://shorturl.fm/UGXRV
https://shorturl.fm/86pz6
https://shorturl.fm/E0888
https://shorturl.fm/RbCjb
https://shorturl.fm/YsH8L
https://shorturl.fm/2x0bv