android - 如果不使用 Card 覆盖,Compose Lottie 组件将无法正常工作

标签 android kotlin android-jetpack-compose lottie

我想在列中包含一个 Lottie 文件,但 lottie 文件超出了其他控件,我可以通过将 lottie 文件放入卡内来解决问题

查看示例

@Composable
fun TileAnimation(modifier: Modifier = Modifier) {
    Card {
        Column {
            Text(
                text = "headline",
                modifier = Modifier
                    .fillMaxWidth()
            )

            val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.lottielogo))
//            Card(
//                modifier = Modifier
//                    .height(183.dp)
//                    .then(modifier),
//                shape = RoundedCornerShape(0.dp)
//            ) {
            LottieAnimation(
                composition = composition,
                modifier = modifier
                    .fillMaxWidth()
                    .height(183.dp),
                contentScale = ContentScale.Crop,
            )
//            }
            Box(modifier = Modifier.fillMaxWidth()) {
                Button(
                    onClick = { }, modifier = Modifier
                        .align(Alignment.Center)
                        .wrapContentSize()
                ) {
                    Text(text = "Button")
                }
            }
        }
    }
}

结果如您所见,文本被覆盖 -> enter image description here

如果你取消注释卡片部分,那么它看起来就很好 -> enter image description here

Kotlin 版本 1.6.10 和 compose 1.1.0,这是 lottie 库 ->

implementation "com.airbnb.android:lottie-compose:4.2.2"

顺便说一句,您可以从here下载Lottie文件 1 : https://lottiefiles.com/96036-bored-cat

我想问我的解决方案是否正确,或者你们建议另一种方法?

最佳答案

默认情况下,Compose 中的所有 View 都不会剪切到边界。

contentScale = ContentScale.Crop 仅确定缩放比例。

要将内容裁剪为其他修饰符指定的 View 大小,请将 Modifier.clip(RectangleShape) 添加到您的 View 。

关于android - 如果不使用 Card 覆盖,Compose Lottie 组件将无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71127025/

相关文章:

java - 全局对象(基于 "extend Application")一夜之间蒸发了..为什么?

java - Android 数据绑定(bind) NoSuchMethodError

android - Kotlin Dagger2 - 找不到符号 DataBindingComponent

kotlin - 在 Kotlin 中获取 lateinit 属性类

android - 在 Android 上, "kill process"是什么意思?

android - Android Studio合并资源集

android - 有没有办法让两个 Activity 同时使用相同的背景?

android - viewpager 内的 RecyclerView

android - 线圈图像缓存不适用于 Jetpack Compose

android - Jetpack Compose 将参数传递给 viewModel