Setting a Lottie animation as a splash activity in Android 12+ involves a few key steps, primarily leveraging the SplashScreen
API introduced in Android 12 (API level 31). Here's a comprehensive guide:
1. Add Dependencies:
- Make sure your project uses Android Gradle Plugin 7.0.0 or higher and Gradle 7.0 or higher.
- Add the necessary dependencies to your module-level
build.gradle
file:
implementation 'androidx.core:core-splashscreen:1.0.1' // Or latest version
implementation 'com.airbnb.android:lottie:latest_version' // Or latest version
}
2. Create Your Lottie Animation:
- Design or obtain your Lottie animation file (
.json
). - Place the animation file in your
res/raw
folder. If theraw
folder doesn't exist, create it.
3. Implement the Splash Screen:
- In your
AndroidManifest.xml
, set the theme of your splash activity to a theme that extendsTheme.SplashScreen
:
<style name="Theme.AppSplash" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">@color/your_splash_background</item>
<item name="windowSplashScreenAnimatedIcon">@drawable/lottie_splash_drawable</item>
<item name="postSplashScreenTheme">@style/Theme.YourApp</item>
</style>
<style name="Theme.YourApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
</style>
</resources>
<application
android:theme="@style/Theme.YourApp">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.AppSplash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
windowSplashScreenBackground
: Sets the background color of the splash screen.windowSplashScreenAnimatedIcon
: This is where you'll use a Drawable that references your Lottie animation.postSplashScreenTheme
: The theme to apply after the splash screen is dismissed.
4. Create a Lottie Drawable:
- Create an XML file in your
res/drawable
folder (e.g.,lottie_splash_drawable.xml
) to define the Lottie drawable:
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
<animated-vector>
<aapt:attr name="android:drawable">
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportWidth="200"
android:viewportHeight="200">
<group>
<aapt:attr name="android:animation">
<objectAnimator
android:duration="2000"
android:propertyName="lottie_rawRes"
android:valueType="rawRes"
android:value="@raw/your_lottie_animation" />
</aapt:attr>
</group>
</vector>
</aapt:attr>
</animated-vector>
</aapt:attr>
</animated-vector>
- Replace
your_lottie_animation
with the name of your Lottie file (without the.json
extension). - Adjust the
width
,height
, andviewportWidth
,viewportHeight
to match your animation's dimensions. - Change the duration of the animation in the
objectAnimator
element.
5. Implement the Splash Screen in your Activity:
- In your
MainActivity
'sonCreate
method, callinstallSplashScreen()
beforesuper.onCreate()
:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
SplashScreen splashScreen = installSplashScreen();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Optionally, you can keep the splash screen visible for a specific duration:
splashScreen.setKeepOnScreenCondition(() -> {
// Return true to keep the splash screen visible.
// Return false to dismiss it.
return false; // For example, dismiss immediately.
});
// Your other activity code...
}
}
installSplashScreen()
: This method sets up the splash screen.setKeepOnScreenCondition()
: This method allows you to control how long the splash screen is displayed. You can use it to perform background tasks or wait for data to load before dismissing the splash screen. If you return false, the splash screen will dismiss immediately after the animated icon has finished playing.
Important Considerations:
- Performance: Optimize your Lottie animation for performance to avoid delays or frame drops.
- Animation Duration: Ensure the animation duration is appropriate for a splash screen. A short, smooth animation is generally preferred.
- API Level: The
SplashScreen
API is available on Android 12 (API level 31) and higher. For older versions, you'll need to use a different approach. - Theme Consistency: Ensure that the splash screen’s background color and the animation’s style match your app’s overall theme.
By following these steps, you can effectively implement a Lottie animation as your splash activity in Android 12+.
Comments
Post a Comment