在后台从 URL 加载图像:Android compose

时间:2021-06-01 14:55:38

标签: android android-jetpack android-jetpack-compose

我正在尝试使用 Coil 伴奏库从网络加载图像。 由于同样的原因,我出现了丢帧现象。

I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.

有没有办法在后台线程中加载图像? 这是我的代码:

 val painter = rememberCoilPainter(request = url)

    Card(modifier = modifier) {
      when (painter.loadState) {
        is ImageLoadState.Success ->
          Image(
            painter = painter,
            contentDescription = title,
            contentScale = ContentScale.FillBounds
          )
        else ->
          Image(
            painter = rememberCoilPainter(request = R.drawable.placeholder),
            contentDescription = title,
            contentScale = ContentScale.FillBounds
          )
      }
    }

这段代码是LazyRow的内容部分,列表中大约有20-25个项目。

请提出一种在后台线程中执行图像加载的方法,以避免掉帧。

1 个答案:

答案 0 :(得分:0)

我不确定这是否足以提高您的表现。

您可以开始改进可以根据请求设置的占位符可绘制对象的使用。
类似的东西:

    Image(
        painter = rememberCoilPainter(
            request = "url",
            requestBuilder = {
                placeholder(R.drawable.placeholder)
            }
        ),
        contentDescription = null)