Jetpack Compose 中的缩放按钮动画

Yan*_*ick 11 android android-jetpack-compose android-compose-button

我想使用 Jetpack Compose 构建这个从 AirBnB 应用程序按下的精彩按钮动画 1

不幸的是,动画/过渡 API 最近发生了变化,而且几乎没有相关的文档。有人可以帮助我找到实现此按钮按下动画的正确方法吗?

编辑 根据 @Amirhosein 的回答,我开发了一个按钮,看起来几乎与 Airbnb 的示例一模一样

代码:

@Composable
fun AnimatedButton() {
    val boxHeight = animatedFloat(initVal = 50f)
    val relBoxWidth = animatedFloat(initVal = 1.0f)
    val fontSize = animatedFloat(initVal = 16f)

    fun animateDimensions() {
        boxHeight.animateTo(45f)
        relBoxWidth.animateTo(0.95f)
       // fontSize.animateTo(14f)
    }

    fun reverseAnimation() {
        boxHeight.animateTo(50f)
        relBoxWidth.animateTo(1.0f)
        //fontSize.animateTo(16f)
    }

        Box(
        modifier = Modifier
            .height(boxHeight.value.dp)
            .fillMaxWidth(fraction = relBoxWidth.value)

            .clip(RoundedCornerShape(8.dp))
            .background(Color.Black)
            .clickable { }
            .pressIndicatorGestureFilter(
                onStart = {
                    animateDimensions()
                },
                onStop = {
                    reverseAnimation()
                },
                onCancel = {
                    reverseAnimation()
                }
            ),
        contentAlignment = Alignment.Center
    ) {
        Text(text = "Explore Airbnb", fontSize = fontSize.value.sp, color = Color.White)
    }
}

Run Code Online (Sandbox Code Playgroud)

视频:

2

不幸的是,我无法弄清楚如何正确地为文本设置动画,因为它目前看起来很糟糕

Cod*_*oet 18

您在寻找这样的东西吗?

@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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用。请注意,我还必须为“MotionEvent.ACTION_CANCEL”设置“selected.value = false”,因此如果我将手指从按钮上拖开而不抬起按钮,动画就会重置。如果没有它,按钮仍然会认为它已“选定”,即使它没有。 (2认同)

Mie*_*źma 11

这是我在项目中使用的实现。对我来说似乎最简洁。

val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
val sizeScale by animateFloatAsState(if (isPressed) 0.5f else 1f)

Button(
    onClick = { },
    modifier = Modifier
        .wrapContentSize()
        .graphicsLayer(
            scaleX = sizeScale,
            scaleY = sizeScale
        ),
    interactionSource = interactionSource
) { Text(text = "Open the reward") }
Run Code Online (Sandbox Code Playgroud)


Ami*_*ein 6

用于pressIndicatorGestureFilter实现此行为。

这是我的解决方法:

@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)
     }
}
Run Code Online (Sandbox Code Playgroud)

默认的jetpack composeButton在其事件中消耗点击手势onClick并且pressIndicatorGestureFilter不接收点击。这就是我创建这个自定义按钮的原因