-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Compose] Add support for ContentScale and Alignment (#1844)
LottieAnimation's behavior now perfectly matches the behavior of ContentScale and Alignment in Image.
- Loading branch information
Showing
7 changed files
with
244 additions
and
23 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
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
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
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
183 changes: 183 additions & 0 deletions
183
...mpose/src/main/java/com/airbnb/lottie/sample/compose/examples/ContentScaleExamplesPage.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,183 @@ | ||
package com.airbnb.lottie.sample.compose.examples | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.border | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.defaultMinSize | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.DropdownMenu | ||
import androidx.compose.material.DropdownMenuItem | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.Text | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowDownward | ||
import androidx.compose.material.icons.filled.ArrowDropDown | ||
import androidx.compose.material.icons.filled.ArrowDropUp | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.PopupProperties | ||
import com.airbnb.lottie.compose.LottieAnimation | ||
import com.airbnb.lottie.compose.LottieCompositionSpec | ||
import com.airbnb.lottie.compose.rememberLottieComposition | ||
import com.airbnb.lottie.sample.compose.R | ||
|
||
@Composable | ||
fun ContentScaleExamplesPage() { | ||
val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.gradient)) | ||
var alignment by remember { mutableStateOf(Alignment.Center) } | ||
var contentScale by remember { mutableStateOf(ContentScale.Fit) } | ||
|
||
UsageExamplePageScaffold { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
) { | ||
OptionsRow( | ||
alignment, | ||
contentScale, | ||
onAlignmentChanged = { alignment = it }, | ||
onContentScaleChanged = { contentScale = it }, | ||
) | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(8.dp) | ||
.border(2.dp, Color.Green) | ||
.padding(2.dp) | ||
) { | ||
LottieAnimation( | ||
composition, | ||
progress = 0f, | ||
alignment = alignment, | ||
contentScale = contentScale, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
@Composable | ||
private fun OptionsRow( | ||
alignment: Alignment, | ||
contentScale: ContentScale, | ||
onContentScaleChanged: (ContentScale) -> Unit, | ||
onAlignmentChanged: (Alignment) -> Unit, | ||
) { | ||
var scaleExpanded by remember { mutableStateOf(false) } | ||
var alignmentExpanded by remember { mutableStateOf(false) } | ||
|
||
val onContentScaleChangedState by rememberUpdatedState(onContentScaleChanged) | ||
val onAlignmentChangedState by rememberUpdatedState(onAlignmentChanged) | ||
|
||
Row( | ||
horizontalArrangement = Arrangement.SpaceAround, | ||
verticalAlignment = Alignment.CenterVertically, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.height(64.dp) | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.clickable { | ||
scaleExpanded = true | ||
alignmentExpanded = false | ||
} | ||
.padding(horizontal = 4.dp, vertical = 16.dp) | ||
){ | ||
Text( | ||
ContentScales[contentScale] ?: "Unknown Content Scale", | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier | ||
.defaultMinSize(minWidth = 128.dp) | ||
) | ||
Icon( | ||
imageVector = if (scaleExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown, | ||
contentDescription = null, | ||
) | ||
DropdownMenu( | ||
scaleExpanded, | ||
onDismissRequest = { scaleExpanded = false }, | ||
) { | ||
ContentScales.forEach { (cs, label) -> | ||
DropdownMenuItem( | ||
onClick = { onContentScaleChangedState(cs) }, | ||
) { | ||
Text(label) | ||
} | ||
} | ||
} | ||
} | ||
Row( | ||
modifier = Modifier | ||
.clickable { | ||
alignmentExpanded = true | ||
scaleExpanded = false | ||
} | ||
.padding(horizontal = 4.dp, vertical = 16.dp) | ||
) { | ||
Text( | ||
Alignments[alignment] ?: "Unknown Alignment", | ||
textAlign = TextAlign.Center, | ||
modifier = Modifier | ||
.defaultMinSize(minWidth = 128.dp) | ||
) | ||
Icon( | ||
imageVector = if (alignmentExpanded) Icons.Filled.ArrowDropUp else Icons.Filled.ArrowDropDown, | ||
contentDescription = null, | ||
) | ||
DropdownMenu( | ||
alignmentExpanded, | ||
onDismissRequest = { alignmentExpanded = false } | ||
) { | ||
Alignments.forEach { (a, label) -> | ||
DropdownMenuItem( | ||
onClick = { onAlignmentChangedState(a) }, | ||
) { | ||
Text(label) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private val Alignments = mapOf( | ||
Alignment.TopStart to "TopStart", | ||
Alignment.TopCenter to "TopCenter", | ||
Alignment.TopEnd to "TopEnd", | ||
Alignment.CenterStart to "CenterStart", | ||
Alignment.Center to "Center", | ||
Alignment.CenterEnd to "CenterEnd", | ||
Alignment.BottomStart to "BottomStart", | ||
Alignment.BottomCenter to "BottomCenter", | ||
Alignment.BottomEnd to "BottomEnd", | ||
) | ||
|
||
private val ContentScales = mapOf( | ||
ContentScale.Fit to "Fit", | ||
ContentScale.Crop to "Crop", | ||
ContentScale.Inside to "Inside", | ||
ContentScale.None to "None", | ||
ContentScale.FillBounds to "FillBounds", | ||
ContentScale.FillHeight to "FillHeight", | ||
ContentScale.FillWidth to "FillWidth", | ||
) |
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
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 @@ | ||
{"v":"5.7.7","fr":29.9700012207031,"ip":0,"op":900.000036657751,"w":500,"h":200,"nm":"Gradient","ddd":0,"assets":[],"layers":[{"ddd":0,"ind":1,"ty":4,"nm":"Shape Layer 1","sr":1,"ks":{"o":{"a":0,"k":100,"ix":11},"r":{"a":0,"k":0,"ix":10},"p":{"a":0,"k":[0,0,0],"ix":2,"l":2},"a":{"a":0,"k":[0,0,0],"ix":1,"l":2},"s":{"a":0,"k":[100,100,100],"ix":6,"l":2}},"ao":0,"shapes":[{"ty":"gr","it":[{"ty":"rc","d":1,"s":{"a":0,"k":[500,200],"ix":2},"p":{"a":0,"k":[0,0],"ix":3},"r":{"a":0,"k":0,"ix":4},"nm":"Rectangle Path 1","mn":"ADBE Vector Shape - Rect","hd":false},{"ty":"st","c":{"a":0,"k":[0.526341557503,0.526341557503,0.526341557503,1],"ix":3},"o":{"a":0,"k":100,"ix":4},"w":{"a":0,"k":18,"ix":5},"lc":1,"lj":1,"ml":4,"bm":0,"nm":"Stroke 1","mn":"ADBE Vector Graphic - Stroke","hd":false},{"ty":"tr","p":{"a":0,"k":[250,100],"ix":2},"a":{"a":0,"k":[0,0],"ix":1},"s":{"a":0,"k":[100,100],"ix":3},"r":{"a":0,"k":0,"ix":6},"o":{"a":0,"k":100,"ix":7},"sk":{"a":0,"k":0,"ix":4},"sa":{"a":0,"k":0,"ix":5},"nm":"Transform"}],"nm":"Rectangle 1","np":2,"cix":2,"bm":0,"ix":1,"mn":"ADBE Vector Group","hd":false},{"ty":"gf","o":{"a":0,"k":100,"ix":10},"r":1,"bm":0,"g":{"p":3,"k":{"a":0,"k":[0,1,0,0,0.5,0.5,0,0.5,1,0,0,1],"ix":9}},"s":{"a":0,"k":[0,0],"ix":5},"e":{"a":0,"k":[500,0],"ix":6},"t":1,"nm":"Gradient Fill 1","mn":"ADBE Vector Graphic - G-Fill","hd":false}],"ip":0,"op":900.000036657751,"st":0,"bm":0}],"markers":[]} |