I have a gif image which needs to be shown as a splash screen in react native for Android device. In MainActivity.java the code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
FrameLayout splashLayout = findViewById(R.id.splash_layout);
ImageView splashImage = findViewById(R.id.splash_image);
Glide.with(this).load(R.raw.splashscreen).into(splashImage);
….
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/splash_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible">
<ImageView
android:id="@+id/splash_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@raw/splashscreen"
android:scaleType="centerCrop" />
</FrameLayout>
</RelativeLayout>
- The problem is after showing the gif image the control is not transferred to App component of react native. It gets stuck and shows a white background if the frame layout is set to hidden.
- Another problem is a white background is shown in-between when traversing from logo to splash screen when the app is opened.
Is there any native way to show gif image as splash screen without using Lottie or any plugin?
2
Answers
In React Native, you can display GIF images using the
react-native-fast-image
library along with thelottie-react-native
library. This combination allows you to display GIFs and control animations easily. Here’s a step-by-step guide on how to display a GIF image in a React Native Android application:Install Dependencies:
First, install the necessary libraries by running the following commands:
Link Native Modules:
You may need to link the native modules for the installed libraries. Run the following command:
Import Libraries:
In your React Native component, import the necessary libraries:
Display GIF Image:
You can now display the GIF image using the
LottieView
component. For example:'./your-gif-file.json'
with the correct path to your GIF file. Lottie expects the GIF in JSON format, so you might need to convert your GIF to the Lottie JSON format using a tool like the LottieFiles website (https://lottiefiles.com/).Start the Development Server:
Start your development server with the following command:
Run the App on Android:
Run your React Native application on an Android emulator or physical Android device using the following command:
This will display the GIF image using the
LottieView
component. Remember to replace'./your-gif-file.json'
with the correct path to your GIF file, and ensure that your GIF is in the Lottie JSON format.You can use react native bootsplash a very decent package that supports animations and offers a lot of customisation!