AsyncImage在LazyColumnKotlinCompose中的使用
创始人
2024-09-21 04:30:52
0
  1. 导入必要的Compose库 implementation "androidx.compose.foundation:foundation:1.0.0-rc02"implementation "androidx.compose.material:material:1.0.0-rc02"

  2. 在需要使用AsyncImage的地方,引入 androidx.compose.foundation.lazy.LazyColumn,例如:

LazyColumn {
    itemsIndexed(images) { _, imageUrl ->
        AsyncImage(
            modifier = Modifier.fillMaxWidth(),
            contentScale = ContentScale.Crop,
            content = {
                CoilImage(
                    data = imageUrl,
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    fadeIn = true,
                    loading = {
                        //显示加载中
                        Box(
                            Modifier
                                .fillMaxSize()
                                .background(Color.LightGray)
                        ) {
                            CircularProgressIndicator(Modifier.align(Alignment.Center))
                        }
                    }
                )
            }
        )
    }
}

这里使用了Coil作为图片加载库。在加载图片时,AsyncImage会显示加载中的进度条。

注意:由于还处于Composable函数编写的过程中,记得使用@Preview注解提供预览效果。

@Preview(showBackground = true)
@Composable
fun PreviewAsyncImage() {
    MyTheme {
        LazyColumn {
            itemsIndexed(images) { _, imageUrl ->
                AsyncImage(
                    modifier = Modifier.fillMaxWidth(),
                    contentScale = ContentScale.Crop,
                    content = {
                        CoilImage(
                            data = imageUrl,
                            contentDescription = null,
                            contentScale = ContentScale.Crop,
                            fadeIn = true,
                            loading = {
                                //显示加载中
                                Box(
                                    Modifier
                                        .fillMaxSize()
                                        .background(Color.LightGray)
                                ) {
                                    CircularProgressIndicator(Modifier.align(Alignment.Center))
                                }
                            }
                        )
                    }
                )
            }
        }
    }
}

其中MyTheme提供了样式和颜色,可以在Theme.kt文件中定义。

@Composable
fun MyTheme(content: @Composable () -> Unit) {
    MaterialTheme(
        colors = Colors,
        typography = Typography,
        content = content

相关内容

热门资讯

透视科技“微乐兰州麻将小程序辅... 透视科技“微乐兰州麻将小程序辅助”素来有辅助开挂安装(有挂技巧);无需打开直接搜索薇:1367043...
重大发现“wepoker可以开... 重大发现“wepoker可以开透视吗”本来有辅助神器(有挂详细);无需打开直接搜索加薇1367043...
推荐一款“创思维激k透视软件”... 创思维激k透视软件是一款专注玩家量身打造的游戏记牌类型软件,在创思维激k透视软件这款游戏中我们可以记...
玩家实测“玉海楼茶苑脚本”原生... 玩家实测“玉海楼茶苑脚本”原生有辅助开挂安装(真实有挂);无需打开直接搜索微信(136704302)...
透视安装“全游大厅拼十辅助”往... 全游大厅拼十辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,全游大厅拼十辅助作为一种经典的娱乐方...
我来教大家“创思维激k透视源码... 大家好,今天小编来为大家解答创思维激k透视源码这个问题咨询软件客服可以免费测试直接加微信(13670...
记者揭秘“途游辅助软件”从来有... 途游辅助软件是一款专注玩家量身打造的游戏记牌类型软件,在途游辅助软件这款游戏中我们可以记录下每张牌的...
如何分辨真伪“江湖悠悠游戏辅助... 【亲,江湖悠悠游戏辅助 这款游戏可以开挂的,确实是有挂的,很多玩家在这款江湖悠悠游戏辅助中打牌都会发...
终于知道“花花生活圈辅助”往昔... 终于知道“花花生活圈辅助”往昔有辅助安装(有挂教程);无需打开直接搜索加薇136704302(咨询了...
信息共享“poker mast... 信息共享“poker master辅助”固有有开挂辅助工具(证实有挂)您好:poker master...