Created
May 16, 2024 07:23
-
-
Save Neeraj1005/59f0b630d16123cf4604dad1471d7d6f to your computer and use it in GitHub Desktop.
React Native Lottie Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import LottieView from "lottie-react-native"; | |
import { Fragment, useRef, useState } from "react"; | |
import { Button, Dimensions, StyleSheet, Text, View } from "react-native"; | |
export default function HomeScreen() { | |
const [playPause, setPlayPause] = useState<string>("play"); | |
const animationRef = useRef<LottieView>(null); | |
const runAnimation = (mode: string) => { | |
if (mode === "play") { | |
animationRef?.current?.play(); | |
} | |
if (mode === "pause") { | |
animationRef?.current?.pause(); | |
} | |
}; | |
return ( | |
<Fragment> | |
<View style={styles.container}> | |
<View style={styles.animationBox}> | |
<LottieView | |
ref={animationRef} | |
source={require("../../assets/animation/animation_1.json")} | |
autoPlay={false} | |
loop={false} | |
style={{ | |
width: Dimensions.get("window").width - 100, | |
height: Dimensions.get("window").height - 20, | |
}} | |
/> | |
</View> | |
<View style={styles.btnBox}> | |
<Button onPress={() => runAnimation("play")} title={"Start"} /> | |
<Button onPress={() => runAnimation("pause")} title={"Pause"} /> | |
</View> | |
</View> | |
</Fragment> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
justifyContent: "center", | |
alignItems: "center", | |
}, | |
animationBox: { | |
flex: 0.7, | |
width: "100%", | |
justifyContent: "center", | |
alignItems: "center", | |
}, | |
btnBox: { | |
flex: 0.3, | |
width: "50%", | |
flexDirection: "row", | |
justifyContent: "space-evenly", | |
alignItems: "center", | |
}, | |
btn: { | |
width: "50%", | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is the output of it