0

我正在使用for加载一个非常大的资源图像,加载需要几十毫秒,第一次加载屏幕时甚至需要数百毫秒。如果图像尚未加载,我不想显示屏幕的某些部分。我徘徊在该方法的实现中,但找不到可以帮助我的东西。CoilJetpack ComposerememberImagePainter

4

2 回答 2

1

您可以通过使用来实现此行为painter.state

val painter = rememberImagePainter(.....)

val painterState = painter.state

现在您可以非常轻松地比较线圈状态,如错误、加载和成功

if (painterState is ImagePainter.State.Loading) {
showLoadingDialog()
}

if(painterState is ImagePainter.State.Success) {
showContent()
}
于 2022-01-31T03:11:55.697 回答
1

有一个选项可以在构建器中传递一个监听器:

rememberImagePainter(data = "url or something", builder = {
    listener(object : ImageRequest.Listener {
        override fun onCancel(request: ImageRequest) {
            ...
        }

        override fun onError(request: ImageRequest, throwable: Throwable) {
            ...
        }

        override fun onStart(request: ImageRequest) {
            ...
        }

        override fun onSuccess(request: ImageRequest, metadata: ImageResult.Metadata) {
            ...
        }
    })
})

但是,如果您只想显示占位符图像,最简单的方法是使用placeholder(...)构建器中也提供的选项:

rememberImagePainter(data = "url or something", builder = {
    placeholder(R.drawable.image_placeholder)
})
于 2022-01-31T08:39:47.747 回答