首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Jetpack Compose中使用Coil显示自定义的可组合占位符?

如何在Jetpack Compose中使用Coil显示自定义的可组合占位符?
EN

Stack Overflow用户
提问于 2021-11-03 00:46:58
回答 1查看 658关注 0票数 0

我需要使用Coil在Jetpack Compose中显示自定义占位符,但该占位符不是可绘制的,它是我自定义的可组合函数。有没有可能用线圈做到这一点?这是我使用Coil的代码片段:

代码语言:javascript
复制
Image(
    modifier = Modifier
        .size(120.dp)
        .align(Alignment.CenterHorizontally),
    painter = rememberImagePainter(
        data = entry.imageUrl,
        builder = {
            crossfade(true)
            MyPlaceholder(resourceId = R.drawable.ic_video)
        },
    ),
    contentDescription = entry.pokemonName
)

这是我的自定义占位符组合函数:

代码语言:javascript
复制
@Composable
fun MyPlaceholder(@DrawableRes resourceId: Int) {
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = Color(0xFFE0E0E0)
    ) {
        Box(
            modifier = Modifier.fillMaxSize(),
            contentAlignment = Alignment.Center,
        ) {
            Surface(
                modifier = Modifier.size(30.dp),
                shape = CircleShape,
                color = Color.White
            ) {
                Image(
                    modifier = Modifier
                        .padding(
                            PaddingValues(
                                start = 11.25.dp,
                                top = 9.25.dp,
                                end = 9.25.dp,
                                bottom = 9.25.dp
                            )
                        )
                        .fillMaxSize(),
                    painter = painterResource(id = resourceId),
                    contentDescription = null
                )
            }
        }
    }
}

我的gradle (线圈):

代码语言:javascript
复制
// Coil
implementation 'io.coil-kt:coil-compose:1.4.0'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-03 01:49:09

Coil没有对可组合占位符的内置支持。

您可以将composable放在Box中,并根据状态在Image上显示占位符。

在我的示例中,如果状态是LoadingError,则显示它。您可以为Error案例添加另一个视图参数,并使用Crossfade代替AnimatedVisibility

另外,我将Modifier.matchParentSize()添加到Image中,以遵循在修改器参数上计算的父大小。您不能将修饰符参数直接传递给Image,因为像align这样修饰符只适用于直接的子代,这就是为什么您总是必须将它传递给容器视图。

代码语言:javascript
复制
@Composable
fun Image(
    painter: ImagePainter,
    placeholder: @Composable () -> Unit,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null,
) {
    Box(modifier) {
        Image(
            painter = painter,
            contentDescription = contentDescription,
            alignment = alignment,
            contentScale = contentScale,
            alpha = alpha,
            colorFilter = colorFilter,
            modifier = Modifier.matchParentSize()
        )

        AnimatedVisibility(
            visible = when (painter.state) {
                is ImagePainter.State.Empty,
                is ImagePainter.State.Success,
                -> false
                is ImagePainter.State.Loading,
                is ImagePainter.State.Error,
                -> true
            }
        ) {
            placeholder()
        }
    }
}

用法:

代码语言:javascript
复制
Image(
    painter = rememberImagePainter(imageUrl),
    placeholder = {
        CustomComposableView(...)
    },
    contentDescription = "...",
    modifier = Modifier
        ...
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69818479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档