Building a Stunning Animated Image Carousel in React Native
Amit Kumar

Amit Kumar @amitkumar13

About: Software Engineer

Location:
New Delhi, India
Joined:
Apr 19, 2024

Building a Stunning Animated Image Carousel in React Native

Publish Date: Feb 15
8 0

Introduction

Creating visually appealing user interfaces is an essential part of modern mobile app development. One way to achieve this is by implementing smooth animations that enhance the user experience. In this tutorial, we will build an elegant image carousel with a background animation using React Native.

Our carousel will display high-quality images that smoothly transition as the user swipes through them. A blurred version of the active image will serve as the background, creating a stunning visual effect.

Image description

Prerequisites

Before diving in, ensure you have the following set up:

  • A working React Native environment
  • Basic knowledge of React Native components and hooks
  • Installed dependencies like react-native-gesture-handler, react-native-reanimated, and react-native-screens (if not already included in your project)

Implementing the Animated Carousel

1. Setting Up the Component

First, let's import the necessary modules and define the component:

import React, {useRef, useCallback} from 'react';
import {
  StatusBar,
  Image,
  Animated,
  View,
  Dimensions,
  StyleSheet,
} from 'react-native';

const {width, height} = Dimensions.get('screen');
const imageW = width * 0.7;
const imageH = imageW * 1.54;
Enter fullscreen mode Exit fullscreen mode

Here, we retrieve the screen dimensions and define image dimensions dynamically.

2. Defining the Image Data

We need an array of image URLs to display in our carousel:

const data = [
  'https://cdn.dribbble.com/users/3281732/screenshots/11192830/media/7690704fa8f0566d572a085637dd1eee.jpg?compress=1&resize=1200x1200',
  'https://cdn.dribbble.com/users/3281732/screenshots/13130602/media/592ccac0a949b39f058a297fd1faa38e.jpg?compress=1&resize=1200x1200',
  'https://cdn.dribbble.com/users/3281732/screenshots/9165292/media/ccbfbce040e1941972dbc6a378c35e98.jpg?compress=1&resize=1200x1200',
  'https://cdn.dribbble.com/users/3281732/screenshots/11205211/media/44c854b0a6e381340fbefe276e03e8e4.jpg?compress=1&resize=1200x1200',
  'https://cdn.dribbble.com/users/3281732/screenshots/7003560/media/48d5ac3503d204751a2890ba82cc42ad.jpg?compress=1&resize=1200x1200',
  'https://cdn.dribbble.com/users/3281732/screenshots/6727912/samji_illustrator.jpeg?compress=1&resize=1200x1200',
  'https://cdn.dribbble.com/users/3281732/screenshots/13661330/media/1d9d3cd01504fa3f5ae5016e5ec3a313.jpg?compress=1&resize=1200x1200',
];
Enter fullscreen mode Exit fullscreen mode

Feel free to add more images or use your own assets.

3. Implementing the Carousel

We will use Animated.FlatList to handle horizontal swiping and an animated background transition.

const CarouselBackgroundAnimation = () => {
  const scrollX = useRef(new Animated.Value(0)).current;

  const renderItem = useCallback(
    ({item}) => (
      <View style={styles.imageContainer}>
        <Image source={{uri: item}} style={styles.image} />
      </View>
    ),
    [],
  );

  return (
    <View style={styles.container}>
      <StatusBar hidden />
      <View style={StyleSheet.absoluteFillObject}>
        {data.map((image, index) => {
          const opacity = scrollX.interpolate({
            inputRange: [
              (index - 1) * width,
              index * width,
              (index + 1) * width,
            ],
            outputRange: [0, 1, 0],
          });
          return (
            <Animated.Image
              key={index}
              source={{uri: image}}
              style={[styles.backgroundImage, {opacity}]}
              blurRadius={50}
            />
          );
        })}
      </View>
      <Animated.FlatList
        data={data}
        renderItem={renderItem}
        keyExtractor={(_, index) => index.toString()}
        horizontal
        pagingEnabled
        showsHorizontalScrollIndicator={false}
        onScroll={Animated.event(
          [{nativeEvent: {contentOffset: {x: scrollX}}}],
          {useNativeDriver: true},
        )}
      />
    </View>
  );
};
Enter fullscreen mode Exit fullscreen mode

4. Styling the Component

To make our carousel visually appealing, we define the following styles:

const styles = StyleSheet.create({
  container: {flex: 1, backgroundColor: '#000'},
  imageContainer: {width, justifyContent: 'center', alignItems: 'center'},
  image: {width: imageW, height: imageH, resizeMode: 'cover', borderRadius: 16},
  backgroundImage: {...StyleSheet.absoluteFillObject},
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we built an animated image carousel with a dynamic background blur effect in React Native. This technique can be used to create engaging UI elements that enhance the user experience.

You can further customize this component by adding autoplay functionality, captions, or even integrating a progress indicator.

Happy coding! 🚀

Comments 0 total

    Add comment