• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.airbnb.lottie.sample.compose.examples
2 
3 import androidx.compose.foundation.layout.Column
4 import androidx.compose.foundation.layout.fillMaxWidth
5 import androidx.compose.foundation.rememberScrollState
6 import androidx.compose.foundation.verticalScroll
7 import androidx.compose.runtime.Composable
8 import androidx.compose.runtime.getValue
9 import androidx.compose.ui.Modifier
10 import com.airbnb.lottie.compose.LottieAnimation
11 import com.airbnb.lottie.compose.LottieCompositionSpec
12 import com.airbnb.lottie.compose.rememberLottieComposition
13 import com.airbnb.lottie.sample.compose.R
14 
15 @Composable
CachingExamplesPagenull16 fun CachingExamplesPage() {
17     UsageExamplePageScaffold {
18         Column(
19             modifier = Modifier
20                 .fillMaxWidth()
21                 .verticalScroll(rememberScrollState())
22         ) {
23             ExampleCard("Default Caching", "Lottie caches compositions by default") {
24                 Example1()
25             }
26             ExampleCard("Day/Night", "Animations in raw/res will automatically respect day and night mode") {
27                 Example2()
28             }
29             ExampleCard("Skip Cache", "Skip the cache") {
30                 Example3()
31             }
32         }
33     }
34 }
35 
36 @Composable
Example1null37 private fun Example1() {
38     // By default, Lottie will cache compositions with a key derived from your LottieCompositionSpec.
39     // If you request the composition multiple times or request it again at some point later, it
40     // will return the previous composition. LottieComposition itself it stateless. All stateful
41     // actions should happen within LottieAnimation.
42     val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.heart))
43     LottieAnimation(composition)
44 }
45 
46 @Composable
Example2null47 private fun Example2() {
48     val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.sun_moon))
49     LottieAnimation(composition)
50 }
51 
52 @Composable
Example3null53 private fun Example3() {
54     val composition by rememberLottieComposition(
55         LottieCompositionSpec.RawRes(R.raw.we_accept_inline_image),
56         // Don't cache this composition. You may want to do this for animations that have images
57         // because the bitmaps are much larger to store than the rest of the animation.
58         cacheKey = null,
59     )
60     LottieAnimation(composition)
61 }