|
| 1 | +package com.threegap.bitnagil.presentation.splash.component.template |
| 2 | + |
| 3 | +import androidx.annotation.RawRes |
| 4 | +import androidx.compose.animation.core.FastOutSlowInEasing |
| 5 | +import androidx.compose.animation.core.animateFloatAsState |
| 6 | +import androidx.compose.animation.core.tween |
| 7 | +import androidx.compose.foundation.layout.fillMaxSize |
| 8 | +import androidx.compose.runtime.Composable |
| 9 | +import androidx.compose.runtime.LaunchedEffect |
| 10 | +import androidx.compose.runtime.getValue |
| 11 | +import androidx.compose.ui.Modifier |
| 12 | +import androidx.compose.ui.draw.alpha |
| 13 | +import androidx.compose.ui.tooling.preview.Preview |
| 14 | +import com.airbnb.lottie.compose.LottieAnimation |
| 15 | +import com.airbnb.lottie.compose.LottieClipSpec |
| 16 | +import com.airbnb.lottie.compose.LottieCompositionSpec |
| 17 | +import com.airbnb.lottie.compose.rememberLottieAnimatable |
| 18 | +import com.airbnb.lottie.compose.rememberLottieComposition |
| 19 | +import com.threegap.bitnagil.designsystem.R |
| 20 | + |
| 21 | +@Composable |
| 22 | +fun BitnagilLottieAnimation( |
| 23 | + @RawRes lottieJson: Int, |
| 24 | + maxFrame: Int, |
| 25 | + onComplete: () -> Unit, |
| 26 | + modifier: Modifier = Modifier, |
| 27 | + onStart: (() -> Unit)? = null, |
| 28 | +) { |
| 29 | + val lottieComposition by rememberLottieComposition(LottieCompositionSpec.RawRes(lottieJson)) |
| 30 | + val lottieAnimatable = rememberLottieAnimatable() |
| 31 | + |
| 32 | + val alpha by animateFloatAsState( |
| 33 | + targetValue = if (lottieComposition != null) 1f else 0f, |
| 34 | + animationSpec = tween( |
| 35 | + durationMillis = 500, |
| 36 | + easing = FastOutSlowInEasing |
| 37 | + ), |
| 38 | + label = "lottieAlpha" |
| 39 | + ) |
| 40 | + |
| 41 | + LaunchedEffect(lottieComposition) { |
| 42 | + lottieComposition?.let { |
| 43 | + onStart?.invoke() |
| 44 | + lottieAnimatable.animate( |
| 45 | + composition = it, |
| 46 | + clipSpec = LottieClipSpec.Frame(0, maxFrame), |
| 47 | + iterations = 1, |
| 48 | + ignoreSystemAnimationsDisabled = true, |
| 49 | + useCompositionFrameRate = true |
| 50 | + ) |
| 51 | + onComplete() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + LottieAnimation( |
| 56 | + composition = lottieComposition, |
| 57 | + progress = { lottieAnimatable.progress }, |
| 58 | + modifier = modifier.alpha(alpha), |
| 59 | + clipToCompositionBounds = false |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +@Preview(showBackground = true) |
| 64 | +@Composable |
| 65 | +private fun BitnagilLottieAnimationPreview() { |
| 66 | + BitnagilLottieAnimation( |
| 67 | + lottieJson = R.raw.splash_lottie, |
| 68 | + maxFrame = 800, |
| 69 | + onComplete = {}, |
| 70 | + modifier = Modifier.fillMaxSize() |
| 71 | + ) |
| 72 | +} |
0 commit comments