generated from KevinnZou/compose-multiplatform-library-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ismail
committed
Jan 3, 2024
1 parent
184f6ca
commit a401226
Showing
26 changed files
with
1,068 additions
and
29 deletions.
There are no files selected for viewing
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
79 changes: 79 additions & 0 deletions
79
lib/src/androidMain/kotlin/image_animation/KottieAnimation.android.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package image_animation | ||
|
||
import androidx.compose.animation.core.Animatable | ||
import androidx.compose.animation.core.FastOutLinearInEasing | ||
import androidx.compose.animation.core.RepeatMode | ||
import androidx.compose.animation.core.repeatable | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.alpha | ||
import com.airbnb.lottie.compose.LottieAnimation | ||
import com.airbnb.lottie.compose.LottieCompositionSpec | ||
import com.airbnb.lottie.compose.animateLottieCompositionAsState | ||
import com.airbnb.lottie.compose.rememberLottieComposition | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.Resource | ||
import org.jetbrains.compose.resources.orEmpty | ||
import org.jetbrains.compose.resources.rememberImageBitmap | ||
import kotlin.math.roundToInt | ||
|
||
@OptIn(ExperimentalResourceApi::class) | ||
@Composable | ||
actual fun KottieAnimation( | ||
modifier: Modifier, | ||
image: Resource, | ||
speed: Float, | ||
iterations: Int, | ||
completed: () -> Unit | ||
) { | ||
|
||
val time = remember { Animatable(0f) } | ||
|
||
LaunchedEffect(Unit) { | ||
time.animateTo( | ||
targetValue = 1f, | ||
animationSpec = repeatable( | ||
iterations = iterations, | ||
animation = tween( | ||
durationMillis = (1000 / speed).roundToInt(), | ||
easing = FastOutLinearInEasing | ||
), | ||
repeatMode = RepeatMode.Restart | ||
) | ||
) | ||
} | ||
|
||
val value = image.rememberImageBitmap() | ||
|
||
Image( | ||
bitmap = value.orEmpty(), | ||
contentDescription = "", | ||
modifier = modifier | ||
.alpha(time.value) | ||
) | ||
|
||
LaunchedEffect( | ||
key1 = time.isRunning, | ||
){ | ||
if (time.isRunning){ | ||
println("animation running!!") | ||
}else{ | ||
if (iterations == 1){ | ||
println("animation completed!!") | ||
completed.invoke() | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |
60 changes: 60 additions & 0 deletions
60
lib/src/androidMain/kotlin/json_animation/KottieAnimation.android.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package json_animation | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import com.airbnb.lottie.compose.LottieCompositionSpec | ||
import com.airbnb.lottie.compose.animateLottieCompositionAsState | ||
import com.airbnb.lottie.compose.rememberLottieComposition | ||
import com.airbnb.lottie.compose.LottieAnimation | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.Resource | ||
|
||
|
||
@OptIn(ExperimentalResourceApi::class) | ||
@SuppressLint("DiscouragedApi") | ||
@Composable | ||
actual fun KottieAnimation( | ||
modifier: Modifier, | ||
fileName: Resource, | ||
speed: Float, | ||
iterations: Int, | ||
completed: () -> Unit | ||
) { | ||
|
||
var animation by remember { mutableStateOf("") } | ||
|
||
LaunchedEffect(Unit){ | ||
animation = fileName.readBytes().decodeToString() | ||
} | ||
|
||
val composition by rememberLottieComposition( | ||
LottieCompositionSpec.JsonString(animation) | ||
) | ||
|
||
val animationState = animateLottieCompositionAsState( | ||
composition = composition, | ||
speed = speed, | ||
iterations = iterations | ||
) | ||
|
||
LaunchedEffect(key1 = animationState.progress){ | ||
if (animationState.progress == 1f){ | ||
completed() | ||
} | ||
} | ||
|
||
LottieAnimation( | ||
composition = composition, | ||
progress = { animationState.progress }, | ||
modifier = Modifier.fillMaxSize(), | ||
) | ||
|
||
} | ||
|
69 changes: 69 additions & 0 deletions
69
lib/src/androidMain/kotlin/text_animation/KottieAnimation.android.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package text_animation | ||
|
||
|
||
import androidx.compose.animation.core.Animatable | ||
import androidx.compose.animation.core.FastOutLinearInEasing | ||
import androidx.compose.animation.core.RepeatMode | ||
import androidx.compose.animation.core.repeatable | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.alpha | ||
import androidx.compose.ui.text.TextStyle | ||
import kotlin.math.roundToInt | ||
|
||
|
||
@Composable | ||
actual fun KottieAnimation( | ||
modifier: Modifier, | ||
text: String, | ||
speed: Float, | ||
iterations: Int, | ||
textStyle: TextStyle, | ||
completed: () -> Unit, | ||
) { | ||
|
||
|
||
val time = remember { Animatable(0f) } | ||
|
||
LaunchedEffect(Unit) { | ||
time.animateTo( | ||
targetValue = 1f, | ||
animationSpec = repeatable( | ||
iterations = iterations, | ||
animation = tween( | ||
durationMillis = (1000 / speed).roundToInt(), | ||
easing = FastOutLinearInEasing | ||
), | ||
repeatMode = RepeatMode.Restart | ||
) | ||
) | ||
} | ||
|
||
Text( | ||
text = text, | ||
style = textStyle, | ||
modifier = modifier | ||
.alpha(time.value) | ||
) | ||
|
||
LaunchedEffect( | ||
key1 = time.isRunning, | ||
){ | ||
if (time.isRunning){ | ||
println("animation running!!") | ||
}else{ | ||
if (iterations == 1){ | ||
println("animation completed!!") | ||
completed.invoke() | ||
} | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
|
44 changes: 44 additions & 0 deletions
44
lib/src/androidMain/kotlin/url_animation/KottieAnimation.android.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package url_animation | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import com.airbnb.lottie.compose.LottieAnimation | ||
import com.airbnb.lottie.compose.LottieCompositionSpec | ||
import com.airbnb.lottie.compose.animateLottieCompositionAsState | ||
import com.airbnb.lottie.compose.rememberLottieComposition | ||
|
||
@Composable | ||
actual fun KottieAnimation( | ||
modifier: Modifier, | ||
url: String, | ||
speed: Float, | ||
iterations: Int, | ||
completed: () -> Unit | ||
) { | ||
|
||
val composition by rememberLottieComposition( | ||
LottieCompositionSpec.Url(url) | ||
) | ||
|
||
val animationState = animateLottieCompositionAsState( | ||
composition = composition, | ||
speed = speed, | ||
iterations = iterations | ||
) | ||
|
||
LaunchedEffect(key1 = animationState.progress){ | ||
if (animationState.progress == 1f){ | ||
completed() | ||
} | ||
} | ||
|
||
LottieAnimation( | ||
composition = composition, | ||
progress = { animationState.progress }, | ||
modifier = Modifier.fillMaxSize(), | ||
) | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
lib/src/commonMain/kotlin/image_animation/KottieAnimation.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package image_animation | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.Resource | ||
|
||
|
||
@OptIn(ExperimentalResourceApi::class) | ||
@Composable | ||
expect fun KottieAnimation( | ||
modifier: Modifier = Modifier, | ||
image: Resource, | ||
speed: Float = 1f, | ||
iterations: Int = 1, | ||
completed: () -> Unit = {} | ||
) | ||
|
19 changes: 19 additions & 0 deletions
19
lib/src/commonMain/kotlin/json_animation/KottieAnimation.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package json_animation | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import org.jetbrains.compose.resources.ExperimentalResourceApi | ||
import org.jetbrains.compose.resources.Resource | ||
|
||
|
||
|
||
@OptIn(ExperimentalResourceApi::class) | ||
@Composable | ||
expect fun KottieAnimation( | ||
modifier: Modifier = Modifier, | ||
fileName: Resource, | ||
speed: Float = 1f, | ||
iterations: Int = 1, | ||
completed: () -> Unit = {} | ||
) | ||
|
20 changes: 20 additions & 0 deletions
20
lib/src/commonMain/kotlin/text_animation/KottieAnimation.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package text_animation | ||
|
||
import androidx.compose.material3.LocalTextStyle | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.TextStyle | ||
|
||
|
||
|
||
@Composable | ||
expect fun KottieAnimation( | ||
modifier: Modifier = Modifier, | ||
text: String, | ||
speed: Float = 1f, | ||
iterations: Int = 1, | ||
textStyle: TextStyle = LocalTextStyle.current, | ||
completed: () -> Unit = {} | ||
) | ||
|
||
|
16 changes: 16 additions & 0 deletions
16
lib/src/commonMain/kotlin/url_animation/KottieAnimation.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package url_animation | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
|
||
|
||
@Composable | ||
expect fun KottieAnimation( | ||
modifier: Modifier = Modifier, | ||
url: String, | ||
speed: Float = 1f, | ||
iterations: Int = 1, | ||
completed: () -> Unit = {} | ||
) | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package utils | ||
|
||
|
||
|
||
object KottieConstants { | ||
val IterateForever = Int.MAX_VALUE | ||
} |
Oops, something went wrong.