Android Jetpack Compose中的缩放按钮动画
我想用Jetpack Compose从AirBnB应用程序中按下这个很棒的按钮动画 不幸的是,动画/过渡API最近发生了更改,几乎没有相关文档。有人能帮我找到正确的方法来实现这个按钮按下动画吗 编辑 基于@Amirhosein answer,我开发了一个按钮,看起来几乎与Airbnb示例一模一样 代码: 视频:Android Jetpack Compose中的缩放按钮动画,android,android-jetpack-compose,Android,Android Jetpack Compose,我想用Jetpack Compose从AirBnB应用程序中按下这个很棒的按钮动画 不幸的是,动画/过渡API最近发生了更改,几乎没有相关文档。有人能帮我找到正确的方法来实现这个按钮按下动画吗 编辑 基于@Amirhosein answer,我开发了一个按钮,看起来几乎与Airbnb示例一模一样 代码: 视频: 不幸的是,我无法确定如何正确设置文本动画,因为它目前看起来非常糟糕请使用按Indicator或sturefilter来实现此行为 以下是我的解决方法: @Preview @Compo
不幸的是,我无法确定如何正确设置文本动画,因为它目前看起来非常糟糕请使用
按Indicator或sturefilter
来实现此行为
以下是我的解决方法:
@Preview
@Composable
fun MyFancyButton() {
val boxHeight = animatedFloat(initVal = 60f)
val boxWidth = animatedFloat(initVal = 200f)
Box(modifier = Modifier
.height(boxHeight.value.dp)
.width(boxWidth.value.dp)
.clip(RoundedCornerShape(4.dp))
.background(Color.Black)
.clickable { }
.pressIndicatorGestureFilter(
onStart = {
boxHeight.animateTo(55f)
boxWidth.animateTo(180f)
},
onStop = {
boxHeight.animateTo(60f)
boxWidth.animateTo(200f)
},
onCancel = {
boxHeight.animateTo(60f)
boxWidth.animateTo(200f)
}
), contentAlignment = Alignment.Center) {
Text(text = "Utforska Airbnb", color = Color.White)
}
}
默认的jetpack compose
按钮
在其onClick
事件中使用点击手势,并且按指示符号或过滤器
不接收点击。这就是我创建此自定义按钮的原因您正在寻找类似的按钮吗
@Composable
fun AnimatedButton() {
val selected = remember { mutableStateOf(false) }
val scale = animateFloatAsState(if (selected.value) 2f else 1f)
Column(
Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(
onClick = { },
modifier = Modifier
.scale(scale.value)
.height(40.dp)
.width(200.dp)
.pointerInteropFilter {
when (it.action) {
MotionEvent.ACTION_DOWN -> {
selected.value = true }
MotionEvent.ACTION_UP -> {
selected.value = false }
}
true
}
) {
Text(text = "Explore Airbnb", fontSize = 15.sp, color = Color.White)
}
}
}
使用
1.0.0-beta06
可以使用修饰符.pointerInput
检测敲击手势。定义枚举:
enum class ComponentState { Pressed, Released }
然后:
好办法!你知道我们如何制作整个表面(包括字体大小)的动画,而不仅仅是框的宽度和高度吗?(请参阅我的更新问题)设置文本字体大小的动画似乎非常简单。你说现在看起来很糟糕是什么意思?
enum class ComponentState { Pressed, Released }
var toState by remember { mutableStateOf(ComponentState.Released) }
val modifier = Modifier.pointerInput(Unit) {
detectTapGestures(
onPress = {
toState = ComponentState.Pressed
tryAwaitRelease()
toState = ComponentState.Released
}
)
}
// Defines a transition of `ComponentState`, and updates the transition when the provided [targetState] changes
val transition: Transition<ComponentState> = updateTransition(targetState = toState, label = "")
// Defines a float animation to scale x,y
val scalex: Float by transition.animateFloat(
transitionSpec = { spring(stiffness = 50f) }, label = ""
) { state ->
if (state == ComponentState.Pressed) 1.25f else 1f
}
val scaley: Float by transition.animateFloat(
transitionSpec = { spring(stiffness = 50f) }, label = ""
) { state ->
if (state == ComponentState.Pressed) 1.05f else 1f
}
Box(
modifier
.padding(16.dp)
.width((100 * scalex).dp)
.height((50 * scaley).dp)
.background(Color.Black, shape = RoundedCornerShape(8.dp)),
contentAlignment = Alignment.Center) {
Text("BUTTON", color = Color.White,
modifier = Modifier.graphicsLayer{
scaleX = scalex;
scaleY = scaley
})
}