You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetpack Compose中使用VectorPainter时Coil占位符不显示

问题解决:AsyncImage矢量图占位符不显示的问题

你的问题出在rememberVectorPainter的使用逻辑以及矢量图尺寸适配的处理上,以下是具体的修正方案:

修正后代码示例

AsyncImage(
    model = createImageUrl(audio.image_path),
    contentDescription = "Song Artwork",
    modifier = Modifier.fillMaxHeight()
        .aspectRatio(1f)
        .padding(horizontal = 4.dp),
    // 替换原rememberVectorPainter写法,用VectorPainter配合remember实现静态占位符
    placeholder = remember { VectorPainter(Icons.Outlined.MusicNote) },
    // 添加contentScale确保矢量图自适应容器尺寸
    contentScale = ContentScale.Fit
)

原因说明

  • rememberVectorPainter设计用于动态修改ImageVector的场景(比如切换图标样式),静态使用内置Icon时,直接用VectorPainter+remember即可满足需求,避免不必要的状态监听开销。
  • 内置Icon的原始尺寸较小(通常为24x24),如果不设置contentScale,占位符会因尺寸不匹配无法在容器中显示,添加contentScale后会自动拉伸或适配容器尺寸。
  • 若上述方案无效,检查你的Coil版本,旧版本对VectorPainter的支持存在兼容问题,升级到最新稳定版即可解决。

内容的提问来源于stack exchange,提问作者Diken Mhrz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:45:49