Glide preload image preload failure problem fixed!

background

There is a requirement in the project: to display an operation guide in the APP. These are three pictures sent by the server.
In order to improve the user experience, you want to download the image first, and then display the image after the download is completed, so that the user does not have to see the blank state when loading the image.

Implementation plan

Here I plan to write a new tool class specifically to handle similar requests. At the same time, Glide's preload method is directly used. After the monitoring resources are ready, the display method is executed. The specific code is as follows:

public class ImagePreloadUtil {
   
    
    
fun preloadImage(imageUrls: List<String?>?, onLoadFinish: () -> Unit) {
   
    
    
    if (imageUrls.isNullOrEmpty()) {
   
    
    
        onLoadFinish()
        return
    }

    // 把一些空的图片地址与 非 http url 地址去掉
    val realImages = imageUrls?.filter {
   
    
     !(it.isNullOrBlank()) && it.isHttpUrl() }

    if (realImages.isNullOrEmpty()) {
   
    
    
        onLoadFinish()
        return
    }

    val countDown = AtomicInteger(realImages?.size ?: 0)
    realImages?.forEach {
   
    
    
        preloadSingleImage(it){
   
    
    
            val result = countDown.decrementAndGet

Guess you like

Origin blog.csdn.net/yztbydh/article/details/130867435