如何测量高度并在 Jetpack Compose 中应用。

huangapple go评论85阅读模式
英文:

How to measure height and apply in view jetpack compose

问题

我想要测量父视图的高度,并相应地进行动画处理。我尝试了这个答案。我觉得它对我返回了0.dp。那么测量项目的正确方法是什么?

实际上,我想要对一个视图进行动画处理,但是当我运行应用程序时,我的视图是0.dp。

@Preview(showBackground = true)
@Composable
fun MoveText() {
    // 代码部分不要翻译
    // ...
}

实际输出

如何测量高度并在 Jetpack Compose 中应用。

期望输出

如何测量高度并在 Jetpack Compose 中应用。

英文:

I want to measure the height of parent view and animate accordingly to that. I tried from this answer. I think it returns 0.dp to me. So what is the proper way of measuring the item?

Actually I want to animate a view, but my view is 0.dp when I run the application.

@Preview(showBackground = true)
@Composable
fun MoveText() {
    val density = LocalDensity.current
    var columnHeightDp by remember {
        mutableStateOf(0.dp)
    }
    var visible by remember { mutableStateOf(true) }
    val iconOffsetAnimation: Dp by animateDpAsState(
        if (visible) 13.dp else 0.dp, tween(1000)
    )
    val textOffsetAnimation: Dp by animateDpAsState(
        if (visible) 6.dp else 0.dp, tween(1000)
    )
    val viewAlpha: Float by animateFloatAsState(
        targetValue = if (visible) 1f else 0f, animationSpec = tween(
            durationMillis = 1000,
        )
    )
    val heightInDp: Dp by animateDpAsState(
        targetValue = if (visible) {
            columnHeightDp
        } else {
            0.dp
        }, animationSpec = tween(
            durationMillis = 1000,
        )
    )
    ScrollComposeTheme {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 16.dp, top = 16.dp)
        ) {
            Column(modifier = Modifier
                .onGloballyPositioned { coordinates ->
                    with(density) {
                        columnHeightDp = coordinates.size.height.toDp()
                    }
                }
                .height(heightInDp)
                .background(Color.LightGray)) {
                Image(
                    modifier = Modifier.padding(top = iconOffsetAnimation),
                    alpha = viewAlpha,
                    imageVector = Icons.Default.ShoppingCart,
                    contentDescription = null,
                )
                Text(
                    modifier = Modifier.padding(top = textOffsetAnimation),
                    text = "Hello, Anna",
                    fontSize = 20.sp,
                    color = Color.Black.copy(alpha = viewAlpha),
                )
            }
            Button(
                modifier = Modifier.padding(top = 10.dp),
                onClick = {
                    visible = !visible
                },
            ) {
                Text(text = "Move Text")
            }
        }
    }
}

Actual Output

如何测量高度并在 Jetpack Compose 中应用。

Expected Output

如何测量高度并在 Jetpack Compose 中应用。

答案1

得分: 1

我试图实现类似于你问题中的这个演示视频的效果。

 val modifier: Modifier = Modifier.background(Color.LightGray)      
 Column(modifier = if (columnHeightDp != 0.dp) {
                modifier
                    .height(heightInDp)
            } else {
                modifier.onSizeChanged {
                    with(density) {
                        columnHeightDp = it.height.toDp()
                    }
                }.wrapContentHeight()
            },
        ) {
         // 你的内容在这里...图片和文本
           }

让我解释一下如何实现这个效果。

modifier = modifier.onSizeChanged {
                with(density) {
                    columnHeightDp = it.height.toDp()
                }
            }.wrapContentHeight()

在这里,对于动态高度的计算,onSizeChanged 方法很有效。所以根据你的需求,我将其保存在一个变量中。在 modifier 中的 wrapContentHeight 方法相当于 XML 中的 WRAP_CONTENT。这将很好地显示两个组件(图片和文本)(按钮)。但我们需要动画,所以我们需要做的是:

if (columnHeightDp != 0.dp) {
                modifier
                    .height(heightInDp)
            }

这将为整个 Column 应用动画,包括 ImageText

英文:

I tried to achieve something like this demo video from your question.

 val modifier: Modifier = Modifier.background(Color.LightGray)      
 Column(modifier = if (columnHeightDp != 0.dp) {
                modifier
                    .height(heightInDp)
            } else {
                modifier.onSizeChanged {
                    with(density) {
                        columnHeightDp = it.height.toDp()
                    }
                }.wrapContentHeight()
            },
        ) {
         // your content goes here...Image and Text
           }

Let me explain how i reach to this.

modifier = modifier.onSizeChanged {
                with(density) {
                    columnHeightDp = it.height.toDp()
                }
            }.wrapContentHeight(),

Here for the dynamic height calculation onSizeChanged method works well. So i save this in a variable as per your need. The wrapContentHeight method in modifier is equivalent as WRAP_CONTENT in xml.
It will work nicely showing both components (Image and Text)(Button). But we need the animation so what we need to do is.

if (columnHeightDp != 0.dp) {
                modifier
                    .height(heightInDp)
            }

This will apply the animation for the entire Column for Image and Text

huangapple
  • 本文由 发表于 2023年3月7日 20:23:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定