Compose CodeLab์ ๋ณด๋ฉฐ ๋ ธ์ ์ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค (์ค์ต ์ฝ๋๋ง ๋ค๋ฆ)
@Composable
funanimateColorAsState(
targetValue: Color, // animation ํ๊ฒ
animationSpec:AnimationSpec<Color> = colorDefaultSpring, // ์๊ฐ์ ๋ฐ๋ผ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐ ์ฌ์ฉํ ์ ๋๋ฉ์ด์
finishedListener: ((Color) -> Unit)? =null // ์ ๋๋ฉ์ด์
์ด ์๋ฃ๋ ๋ ์๋ฆผ์ ๋ฐ๋ ์ ํ์ ๋ฆฌ์ค๋
):State<Color> {
valconverter = remember(targetValue.colorSpace){
(Color.VectorConverter)(targetValue.colorSpace)
}
}
var clicked by remember {
mutableStateOf(false)
}
val buttonColors by animateColorAsState(if (clicked) Color.Yellow else Color.DarkGray)
Button(
onClick = { clicked = !clicked },
colors = ButtonDefaults.buttonColors(backgroundColor = buttonColors)
)
{
Text(text = if (clicked)"click" else "cancel", color = if(!clicked)Color.White else Color.Black)
}
Button(
onClick = { clicked = !clicked },
colors = ButtonDefaults.buttonColors(backgroundColor = buttonColors)
) {
Text(text = if (clicked)"click" else "cancel", color = if (!clicked)Color.White else Color.Black)
}
AnimatedVisibility(visible = !clicked) {
Box(
modifier = Modifier.fillMaxWidth().background(color = MaterialTheme.colors.secondary).clip(RoundedCornerShape(20.dp)).padding(10.dp)
) {
Text(
text = "you clicked!!",
color = Color.White
)
}
}
enterTransition
, exitTransition
, initialOffsetY
, targetOffsetY
, animationSpec
๋ฑ์ ๋งค๊ฐ๋ณ์๋ฅผ ํตํ ์ปค์คํ
์ ๋๋ฉ์ด์
๊ฐ๋ฅanimationSpec
์ด๋?@Composable
fun<T> updateTransition(
targetState: T,
label: String? =null
): Transition<T> {
valtransition = remember{Transition(targetState, label = label)}
transition.animateTo(targetState)
DisposableEffect(transition){
onDispose{
transition.onTransitionEnd()
}
}
returntransition
}
var clicked by remember {
mutableStateOf(true)
}
val transition = updateTransition(targetState = clicked, label = "")
val buttonColors by transition.animateColor(label = "") { clicked ->
if (clicked) Color.Yellow else Color.DarkGray
}
val textColors by ta
Button(
onClick = { clicked = !clicked },
colors = ButtonDefaults.buttonColors(buttonColors)
) {
Text(text = if (clicked)"click button!" else "cancel", color = if (!clicked)Color.White else Color.Black, modifier = Modifier.animateContentSize())
rememberInfiniteTransition
๋ฅผ ์ด์ฉํด ์ ์ธanimate*
ํ์ฅํจ์๋ฅผ ํตํด ์ ๋๋ฉ์ด์
๊ฐ ๋ณ๊ฒฝcolor์ infiniteTransition
์ ์ฉํ๊ธฐ!
val infiniteTransition = rememberInfiniteTransition()
val buttonColors by infiniteTransition.animateColor(
initialValue = Color.Yellow,
targetValue = Color.DarkGray,
animationSpec = infiniteRepeatable(
animation = keyframes {
durationMillis = 1000
},
repeatMode = RepeatMode.Restart
)
)
repeatMode = RepeatMode.Reverse
repeatMode = RepeatMode.Restart
[pointerInput](https://developer.android.com/reference/kotlin/androidx/compose/ui/input/pointer/package-summary?hl=ko#(androidx.compose.ui.Modifier).pointerInput(kotlin.Any,kotlin.coroutines.SuspendFunction1))
์์ ์ ์ฌ์ฉ์ ์ฌ์ฉ์๊ฐ ๋๋๊ทธํ๋ ์๋๋ฅผ ์ถ์ ๊ฐ๋ฅ