【问题标题】:Showing a Progress bar while loading image using Coil?使用线圈加载图像时显示进度条?
【发布时间】:2021-10-26 12:06:38
【问题描述】:

Coil中的URL获取图片时如何显示进度条?

【问题讨论】:

    标签: android kotlin image-loading coil


    【解决方案1】:

    我只在 Compose 中尝试过,但我认为您可以使用它like Glide

    设置进度条为占位符

    val circularProgressDrawable = CircularProgressDrawable(this)
    circularProgressDrawable.strokeWidth = 5f
    circularProgressDrawable.centerRadius = 30f
    circularProgressDrawable.start()
    
    imageView.load("https://www.example.com/image.jpg") {
        crossfade(true)
        placeholder(circularProgressDrawable)
        transformations(CircleCropTransformation())
    }
    

    【讨论】:

      【解决方案2】:

      我没有和 Coil 合作,但我建议你可以做下一个:

      // Before request run on the needed method
      yourProgressbar.visibility = View.VISIBLE
      
      val request = ImageRequest.Builder(context)
      .data("https://www.example.com/image.jpg")
      .target { drawable ->
          yourProgressbar.visibility = View.INVISIBLE
      }.build()   
      
      val disposable = imageLoader.enqueue(request)
      

      这里是official doc's example

      【讨论】:

        【解决方案3】:

        使用ImageRequest.Listener

        例子:

        val imageRequest = ImageRequest.Builder(context)
            .data(url)
            .listener(
                onStart = {
                    // set your progressbar visible here
                },
                onSuccess = { request, metadata ->
                    // set your progressbar invisible here
                }
            )
            .build()
        
        imageLoader.enqueue(request)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-20
          • 2011-06-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多