Compose-Coil基础上手

准备阶段

Coil与Glide、Picasso为三大图片库

Coil 官方中文文档:https://coil-kt.github.io/coil/README-zh/

下载依赖:

// 原生
implementation("io.coil-kt:coil:2.2.2")

// compose
implementation("io.coil-kt:coil-compose:2.2.2")

由于 Coil 使用 okhttp 请求,故需要在 manifest.xml 添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />


分步快速上手

全部参考官方文档并做精简处理


AsyncImage

图片会自适应所有布局

可根据当前盒子大小按照原比例拉伸图片,并保证图片宽度占满整个盒子宽度(在不对 AsyncImage 设置任何 modifier 的情况下)

@Composable
fun CoilBasic(){
    // 异步加载网络图片,使用内置okhttp请求
    // 这是一张普通的苹果图片
    AsyncImage(
        model = "https://img0.baidu.com/it/u=4" +
            "023062467,4103551361&fm=253&fmt=auto&app=1" +
            "38&f=JPEG?w=650&h=434",
        contentDescription = null)
}

或者直接在 Image 组件中使用

rememberAsyncImagePainter 接收图片 URL 作为参数

@Composable
fun CoilImage(){
    Image(
        ...
        painter = rememberAsyncImagePainter(model = IMAGE_URL),
        contentDescription = null
    )
}

更深入的 AsyncImage 配置

@Composable
fun CoilAsyncDefine(){
    AsyncImage(
        // ImageRequest.Builder构建自定义图片显示方式
        model = ImageRequest.Builder(LocalContext.current)
            .data("https://pic-go-bed.oss-cn-beijing.aliyuncs.com/img/20220316151929.png")
            .crossfade(true)
            .build(),

        // 其他的不怎么重要的设置
        contentDescription = stringResource(R.string.description),
        placeholder = painterResource(id = R.drawable.place_holder),
        error = painterResource(id = R.drawable.error),
        onSuccess = {
            Log.d(TAG, "success")
        }
    )
}

SubcomposeAsyncImage

该组件为 AsyncImage 的变体,可以轻易地通过 loading 属性设置加载时动画

SubcomposeAsyncImage(
    model = "https://example.com/image.jpg",
    loading = {
        CircularProgressIndicator()
    },
    contentDescription = stringResource(R.string.description)
)

可以直接检查 painter 状态实现更复杂的逻辑

SubcomposeAsyncImage(
    model = "https://example.com/image.jpg",
    contentDescription = stringResource(R.string.description)
) {
    val state = painter.state
    if (state is AsyncImagePainter.State.Loading || state is AsyncImagePainter.State.Error) {
        CircularProgressIndicator()
    } else {
        SubcomposeAsyncImageContent()
    }
}

AsyncImagePainter

若无法使用 AsyncImage 组件,可以使用 AsyncImagePainter 代替

val painter = rememberAsyncImagePainter(
  model = ImageRequest.Builder(LocalContext.current)
    .data("https://pic-go-bed.oss-cn-beijing.aliyuncs.com/img/20220316151929.png")
    .build()
)
if (painter.state is AsyncImagePainter.State.Loading) {
  CircularProgressIndicator()
}
Image(
  painter = painter,
  contentDescription = stringResource(R.string.description)
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zhillery

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值