Updated FAB animations
This commit is contained in:
1 parent
b02bb044a3
commit
c1de9ec1a9
3 files changed
+146
-148
No files matched your search
+1
-2
@@ -130,8 +130,7 @@ fun AddAlbumScreen(
|
||||
onFolderClick = {
|
||||
selectionMode = false
|
||||
folderPickerLauncher.launch(null)
|
||||
},
|
||||
animate = animate
|
||||
}
|
||||
)
|
||||
},
|
||||
content = {
|
||||
|
||||
+144
-144
@@ -1,9 +1,10 @@
|
||||
package com.anthonyla.paperize.feature.wallpaper.presentation.add_album_screen.components
|
||||
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.animation.AnimatedContent
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.animateContentSize
|
||||
import androidx.compose.animation.core.FastOutSlowInEasing
|
||||
import androidx.compose.animation.core.LinearEasing
|
||||
import androidx.compose.animation.core.Spring
|
||||
import androidx.compose.animation.core.animateDp
|
||||
import androidx.compose.animation.core.spring
|
||||
@@ -11,10 +12,14 @@ import androidx.compose.animation.core.tween
|
||||
import androidx.compose.animation.core.updateTransition
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.animation.scaleIn
|
||||
import androidx.compose.animation.scaleOut
|
||||
import androidx.compose.animation.togetherWith
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
@@ -24,12 +29,13 @@ import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.Folder
|
||||
import androidx.compose.material.icons.filled.PhotoLibrary
|
||||
import androidx.compose.material.icons.filled.Remove
|
||||
import androidx.compose.material3.CardColors
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.ElevatedCard
|
||||
import androidx.compose.material3.FloatingActionButtonDefaults
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.LargeFloatingActionButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.surfaceColorAtElevation
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
@@ -38,12 +44,9 @@ import androidx.compose.runtime.remember
|
||||
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.input.pointer.pointerInput
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.anthonyla.paperize.R
|
||||
import com.anthonyla.paperize.feature.wallpaper.presentation.library_screen.components.MenuRow
|
||||
|
||||
/**
|
||||
* Animated FAB that expands to show two options: Add image and Add folder
|
||||
@@ -51,173 +54,170 @@ import com.anthonyla.paperize.feature.wallpaper.presentation.library_screen.comp
|
||||
*/
|
||||
@Composable
|
||||
fun AddAlbumAnimatedFab(
|
||||
animate: Boolean,
|
||||
onImageClick: () -> Unit,
|
||||
onFolderClick: () -> Unit
|
||||
) {
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
val fabTransition = updateTransition(targetState = expanded, label = "")
|
||||
val expandedWidth = 165.dp
|
||||
val itemSize = 2
|
||||
val menuColor = MaterialTheme.colorScheme.surfaceColorAtElevation(5.dp)
|
||||
val fabColor = MaterialTheme.colorScheme.primaryContainer
|
||||
var isFabExpanded by remember { mutableStateOf(false) }
|
||||
BackHandler(isFabExpanded) { isFabExpanded = false }
|
||||
|
||||
BackHandler(expanded) { expanded = false }
|
||||
val fabTransition = updateTransition(targetState = isFabExpanded, label = "fabTransition")
|
||||
|
||||
val expandHorizontalSize = Pair(165.dp, 90.dp)
|
||||
val expandHorizontally by fabTransition.animateDp(
|
||||
transitionSpec = { spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow) }, label = "",
|
||||
targetValueByState = { isExpanded ->
|
||||
if (isExpanded) expandHorizontalSize.first else expandHorizontalSize.second
|
||||
val expandFabHorizontally by fabTransition.animateDp(
|
||||
transitionSpec = {
|
||||
spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow)
|
||||
},
|
||||
label = "expandVertically",
|
||||
targetValueByState = {isExpanded ->
|
||||
if (isExpanded) expandedWidth else 96.dp
|
||||
}
|
||||
)
|
||||
|
||||
val shrinkVerticalSize = Pair(50.dp, 90.dp)
|
||||
val shrinkVertically by fabTransition.animateDp(
|
||||
transitionSpec = { spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow) }, label = "",
|
||||
targetValueByState = { isExpanded ->
|
||||
if (isExpanded) shrinkVerticalSize.first else shrinkVerticalSize.second
|
||||
val shrinkFabVertically by fabTransition.animateDp(
|
||||
transitionSpec = {
|
||||
spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow)
|
||||
},
|
||||
label = "shrinkHorizontally",
|
||||
targetValueByState = {isExpanded ->
|
||||
if (isExpanded) 60.dp else 96.dp
|
||||
}
|
||||
)
|
||||
|
||||
val menuHeightSize = Pair(100.dp, 90.dp)
|
||||
val menuHeight by fabTransition.animateDp(
|
||||
transitionSpec = { spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow) }, label = "",
|
||||
targetValueByState = { isExpanded ->
|
||||
if (isExpanded) menuHeightSize.first else menuHeightSize.second
|
||||
transitionSpec = {
|
||||
spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow)
|
||||
},
|
||||
label = "menuHeight",
|
||||
targetValueByState = {isExpanded ->
|
||||
if (isExpanded) (itemSize * 50).dp else 0.dp
|
||||
}
|
||||
)
|
||||
|
||||
if (animate) {
|
||||
ElevatedCard(
|
||||
colors = CardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(5.dp),
|
||||
contentColor = Color.Transparent,
|
||||
disabledContentColor = Color.Transparent,
|
||||
disabledContainerColor = Color.Transparent,
|
||||
),
|
||||
shape = RoundedCornerShape(28.dp),
|
||||
modifier = Modifier
|
||||
.padding(4.dp)
|
||||
.width(expandHorizontally)
|
||||
.pointerInput(Unit) {
|
||||
detectTapGestures(onTap = { expanded = false })
|
||||
}
|
||||
ElevatedCard(
|
||||
modifier = Modifier.padding(4.dp),
|
||||
shape = RoundedCornerShape(28.dp),
|
||||
elevation = CardDefaults.elevatedCardElevation(4.dp),
|
||||
colors = CardDefaults.elevatedCardColors().copy(
|
||||
containerColor = menuColor
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.SpaceEvenly,
|
||||
modifier = Modifier.height(menuHeight)
|
||||
) {
|
||||
AnimatedVisibility(
|
||||
visible = expanded,
|
||||
enter = fadeIn(tween(300, 0, FastOutSlowInEasing)),
|
||||
exit = fadeOut(tween(300, 0, FastOutSlowInEasing)),
|
||||
modifier = Modifier.animateContentSize(tween(50, 0, FastOutSlowInEasing)),
|
||||
visible = isFabExpanded,
|
||||
label = stringResource(R.string.add_images_btn),
|
||||
enter = fadeIn(tween(300, 150, LinearEasing)),
|
||||
exit = fadeOut(tween(200, 50, LinearEasing)) + scaleOut(),
|
||||
) {
|
||||
Surface(tonalElevation = 5.dp) {
|
||||
Column(modifier = Modifier.height(menuHeight)) {
|
||||
MenuRow(
|
||||
text = stringResource(R.string.add_images_btn),
|
||||
icon = Icons.Filled.PhotoLibrary,
|
||||
horizontalExpansion = expandHorizontally,
|
||||
onClick = {
|
||||
expanded = false
|
||||
onImageClick()
|
||||
}
|
||||
)
|
||||
MenuRow(
|
||||
text = stringResource(R.string.add_folder_btn),
|
||||
icon = Icons.Filled.Folder,
|
||||
horizontalExpansion = expandHorizontally,
|
||||
onClick = {
|
||||
expanded = false
|
||||
onFolderClick()
|
||||
}
|
||||
)
|
||||
}
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.width(expandFabHorizontally)
|
||||
.clickable {
|
||||
onImageClick()
|
||||
isFabExpanded = false
|
||||
},
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.Filled.PhotoLibrary,
|
||||
contentDescription = stringResource(R.string.add_images_btn),
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
modifier = Modifier.padding(start = 16.dp, top = 12.dp, bottom = 12.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
Text(
|
||||
text = stringResource(R.string.add_images_btn),
|
||||
modifier = Modifier.padding(end = 16.dp, top = 12.dp, bottom = 12.dp),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
tonalElevation = 3.dp,
|
||||
AnimatedVisibility(
|
||||
visible = isFabExpanded,
|
||||
label = stringResource(R.string.add_folder_btn),
|
||||
enter = fadeIn(tween(300, 150, LinearEasing)),
|
||||
exit = fadeOut(tween(200, 50, LinearEasing)) + scaleOut(),
|
||||
) {
|
||||
LargeFloatingActionButton(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
onClick = { expanded = !expanded },
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.width(expandHorizontally)
|
||||
.height(shrinkVertically)
|
||||
.animateContentSize(),
|
||||
.width(expandFabHorizontally)
|
||||
.clickable {
|
||||
onFolderClick()
|
||||
isFabExpanded = false
|
||||
},
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
if (!expanded) Icons.Filled.Add else Icons.Filled.Remove,
|
||||
contentDescription = stringResource(R.string.add_wallpaper),
|
||||
modifier = Modifier.padding(16.dp),
|
||||
)
|
||||
}
|
||||
Icon(
|
||||
imageVector = Icons.Filled.Folder,
|
||||
contentDescription = stringResource(R.string.add_folder_btn),
|
||||
modifier = Modifier.padding(start = 16.dp, top = 12.dp, bottom = 12.dp),
|
||||
tint = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
Spacer(modifier = Modifier.weight(1f))
|
||||
Text(
|
||||
text = stringResource(R.string.add_folder_btn),
|
||||
modifier = Modifier.padding(end = 16.dp, top = 12.dp, bottom = 12.dp),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
ElevatedCard(
|
||||
colors = CardColors(
|
||||
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(5.dp),
|
||||
contentColor = Color.Transparent,
|
||||
disabledContentColor = Color.Transparent,
|
||||
disabledContainerColor = Color.Transparent,
|
||||
),
|
||||
shape = RoundedCornerShape(28.dp),
|
||||
|
||||
LargeFloatingActionButton(
|
||||
elevation = FloatingActionButtonDefaults.elevation(4.dp),
|
||||
shape = RoundedCornerShape(28),
|
||||
containerColor = fabColor,
|
||||
onClick = { isFabExpanded = !isFabExpanded },
|
||||
modifier = Modifier
|
||||
.padding(4.dp)
|
||||
.width(if (expanded) expandHorizontalSize.first else expandHorizontalSize.second)
|
||||
.pointerInput(Unit) {
|
||||
detectTapGestures(onTap = { expanded = false })
|
||||
}
|
||||
.width(expandFabHorizontally)
|
||||
.height(shrinkFabVertically)
|
||||
) {
|
||||
if (expanded) {
|
||||
Surface(tonalElevation = 5.dp) {
|
||||
Column(modifier = Modifier.height(if (expanded) menuHeightSize.first else menuHeightSize.second)) {
|
||||
MenuRow(
|
||||
text = stringResource(R.string.add_images_btn),
|
||||
icon = Icons.Filled.PhotoLibrary,
|
||||
horizontalExpansion = if (expanded) expandHorizontalSize.first else expandHorizontalSize.second,
|
||||
onClick = {
|
||||
expanded = false
|
||||
onImageClick()
|
||||
}
|
||||
)
|
||||
MenuRow(
|
||||
text = stringResource(R.string.add_folder_btn),
|
||||
icon = Icons.Filled.Folder,
|
||||
horizontalExpansion = if (expanded) expandHorizontalSize.first else expandHorizontalSize.second,
|
||||
onClick = {
|
||||
expanded = false
|
||||
onFolderClick()
|
||||
}
|
||||
)
|
||||
AnimatedContent(
|
||||
isFabExpanded,
|
||||
transitionSpec = {
|
||||
when (targetState) {
|
||||
true -> {
|
||||
(
|
||||
scaleIn(
|
||||
tween(100, 300, FastOutSlowInEasing)
|
||||
) + fadeIn(
|
||||
tween(250, 300, LinearEasing)
|
||||
)
|
||||
) .togetherWith(
|
||||
fadeOut(
|
||||
tween(10, 0, LinearEasing)
|
||||
)
|
||||
)
|
||||
}
|
||||
false -> {
|
||||
fadeIn(
|
||||
tween(250, 200, FastOutSlowInEasing)
|
||||
).togetherWith(
|
||||
fadeOut(
|
||||
tween(10, 0, LinearEasing),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
tonalElevation = 3.dp,
|
||||
) {
|
||||
LargeFloatingActionButton(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
onClick = { expanded = !expanded },
|
||||
modifier = Modifier
|
||||
.width(if (expanded) expandHorizontalSize.first else expandHorizontalSize.second)
|
||||
.height(if (expanded) shrinkVerticalSize.first else shrinkVerticalSize.second)
|
||||
) {
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
Icon(
|
||||
if (!expanded) Icons.Filled.Add else Icons.Filled.Remove,
|
||||
contentDescription = stringResource(R.string.add_wallpaper),
|
||||
modifier = Modifier.padding(16.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
label = "FabContent"
|
||||
) { isExpanded ->
|
||||
Icon(
|
||||
imageVector = if (isExpanded) Icons.Filled.Remove else Icons.Filled.Add,
|
||||
contentDescription = stringResource(R.string.add_wallpaper),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+1
-2
@@ -126,8 +126,7 @@ fun AlbumViewScreen(
|
||||
onFolderClick = {
|
||||
selectionMode = false
|
||||
folderPickerLauncher.launch(null)
|
||||
},
|
||||
animate = animate
|
||||
}
|
||||
)
|
||||
},
|
||||
content = { it ->
|
||||
|
||||
Reference in new issue
Block a user