英文:
How to measure height and apply in view jetpack compose
问题
我想要测量父视图的高度,并相应地进行动画处理。我尝试了这个答案。我觉得它对我返回了0.dp。那么测量项目的正确方法是什么?
实际上,我想要对一个视图进行动画处理,但是当我运行应用程序时,我的视图是0.dp。
@Preview(showBackground = true)
@Composable
fun MoveText() {
// 代码部分不要翻译
// ...
}
实际输出
期望输出
英文:
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
Expected Output
答案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
应用动画,包括 Image
和 Text
。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论