<lambda>null1 package com.airbnb.lottie.sample.compose.examples
2
3 import androidx.compose.foundation.border
4 import androidx.compose.foundation.clickable
5 import androidx.compose.foundation.layout.Arrangement
6 import androidx.compose.foundation.layout.Box
7 import androidx.compose.foundation.layout.Column
8 import androidx.compose.foundation.layout.Row
9 import androidx.compose.foundation.layout.defaultMinSize
10 import androidx.compose.foundation.layout.fillMaxSize
11 import androidx.compose.foundation.layout.fillMaxWidth
12 import androidx.compose.foundation.layout.height
13 import androidx.compose.foundation.layout.padding
14 import androidx.compose.material.DropdownMenu
15 import androidx.compose.material.DropdownMenuItem
16 import androidx.compose.material.Icon
17 import androidx.compose.material.Text
18 import androidx.compose.material.icons.Icons
19 import androidx.compose.material.icons.filled.ArrowDropDown
20 import androidx.compose.material.icons.filled.ArrowDropUp
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.mutableStateOf
24 import androidx.compose.runtime.remember
25 import androidx.compose.runtime.rememberUpdatedState
26 import androidx.compose.runtime.setValue
27 import androidx.compose.ui.Alignment
28 import androidx.compose.ui.Modifier
29 import androidx.compose.ui.graphics.Color
30 import androidx.compose.ui.layout.ContentScale
31 import androidx.compose.ui.text.style.TextAlign
32 import androidx.compose.ui.unit.dp
33 import com.airbnb.lottie.compose.LottieAnimation
34 import com.airbnb.lottie.compose.LottieCompositionSpec
35 import com.airbnb.lottie.compose.rememberLottieComposition
36 import com.airbnb.lottie.sample.compose.R
37
38 @Composable
39 fun ContentScaleExamplesPage() {
40 val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.gradient))
41 var alignment by remember { mutableStateOf(Alignment.Center) }
42 var contentScale by remember { mutableStateOf(ContentScale.Fit) }
43
44 UsageExamplePageScaffold {
45 Column(
46 modifier = Modifier
47 .fillMaxWidth()
48 ) {
49 OptionsRow(
50 alignment,
51 contentScale,
52 onAlignmentChanged = { alignment = it },
53 onContentScaleChanged = { contentScale = it },
54 )
55 Box(
56 modifier = Modifier
57 .fillMaxSize()
58 .padding(8.dp)
59 .border(2.dp, Color.Green)
60 .padding(2.dp)
61 ) {
62 LottieAnimation(
63 composition,
64 progress = { 0f },
65 alignment = alignment,
66 contentScale = contentScale,
67 )
68 }
69 }
70 }
71 }
72 @Composable
OptionsRownull73 private fun OptionsRow(
74 alignment: Alignment,
75 contentScale: ContentScale,
76 onContentScaleChanged: (ContentScale) -> Unit,
77 onAlignmentChanged: (Alignment) -> Unit,
78 ) {
79 var scaleExpanded by remember { mutableStateOf(false) }
80 var alignmentExpanded by remember { mutableStateOf(false) }
81
82 val onContentScaleChangedState by rememberUpdatedState(onContentScaleChanged)
83 val onAlignmentChangedState by rememberUpdatedState(onAlignmentChanged)
84
85 Row(
86 horizontalArrangement = Arrangement.SpaceAround,
87 verticalAlignment = Alignment.CenterVertically,
88 modifier = Modifier
89 .fillMaxWidth()
90 .height(64.dp)
91 ) {
92 Row(
93 modifier = Modifier
94 .clickable {
95 scaleExpanded = true
96 alignmentExpanded = false
97 }
98 .padding(horizontal = 4.dp, vertical = 16.dp)
99 ){
100 Text(
101 ContentScales[contentScale] ?: "Unknown Content Scale",
102 textAlign = TextAlign.Center,
103 modifier = Modifier
104 .defaultMinSize(minWidth = 128.dp)
105 )
106 Icon(
107 imageVector = if (scaleExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown,
108 contentDescription = null,
109 )
110 DropdownMenu(
111 scaleExpanded,
112 onDismissRequest = { scaleExpanded = false },
113 ) {
114 ContentScales.forEach { (cs, label) ->
115 DropdownMenuItem(
116 onClick = { onContentScaleChangedState(cs) },
117 ) {
118 Text(label)
119 }
120 }
121 }
122 }
123 Row(
124 modifier = Modifier
125 .clickable {
126 alignmentExpanded = true
127 scaleExpanded = false
128 }
129 .padding(horizontal = 4.dp, vertical = 16.dp)
130 ) {
131 Text(
132 Alignments[alignment] ?: "Unknown Alignment",
133 textAlign = TextAlign.Center,
134 modifier = Modifier
135 .defaultMinSize(minWidth = 128.dp)
136 )
137 Icon(
138 imageVector = if (alignmentExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown,
139 contentDescription = null,
140 )
141 DropdownMenu(
142 alignmentExpanded,
143 onDismissRequest = { alignmentExpanded = false }
144 ) {
145 Alignments.forEach { (a, label) ->
146 DropdownMenuItem(
147 onClick = { onAlignmentChangedState(a) },
148 ) {
149 Text(label)
150 }
151 }
152 }
153 }
154 }
155 }
156
157 private val Alignments = mapOf(
158 Alignment.TopStart to "TopStart",
159 Alignment.TopCenter to "TopCenter",
160 Alignment.TopEnd to "TopEnd",
161 Alignment.CenterStart to "CenterStart",
162 Alignment.Center to "Center",
163 Alignment.CenterEnd to "CenterEnd",
164 Alignment.BottomStart to "BottomStart",
165 Alignment.BottomCenter to "BottomCenter",
166 Alignment.BottomEnd to "BottomEnd",
167 )
168
169 private val ContentScales = mapOf(
170 ContentScale.Fit to "Fit",
171 ContentScale.Crop to "Crop",
172 ContentScale.Inside to "Inside",
173 ContentScale.None to "None",
174 ContentScale.FillBounds to "FillBounds",
175 ContentScale.FillHeight to "FillHeight",
176 ContentScale.FillWidth to "FillWidth",
177 )
178