'How to ensure that glide uses the palette to obtain color and fill it for a period of time before loading a successful picture

I want to use palette to get a color and fill it before glide successfully loads the picture.

Just like bing.com or twitter.com, a color will cover the image before it is successfully loaded.

I use the following methods to achieve it:

    Glide
    .with(imageView.context)
    .asBitmap()
    .load(imageUrl)
    .listener(object : RequestListener<Bitmap> {
        override fun onLoadFailed(e: GlideException?, model: Any, target: Target<Bitmap>, isFirstResource: Boolean) = false

        override fun onResourceReady(resource: Bitmap, model: Any, target: Target<Bitmap>, dataSource: DataSource, isFirstResource: Boolean): Boolean {


            Palette
            .from(resource)
            .generate(PaletteAsyncListener { palette ->
                val darkVibrantSwatch = palette!!.darkVibrantSwatch
                val dominantSwatch = palette.dominantSwatch
                val lightVibrantSwatch = palette.lightVibrantSwatch
                if (darkVibrantSwatch != null) {
                    imageView.setBackgroundColor(darkVibrantSwatch.rgb)
                } else if (dominantSwatch != null) {
                    imageView.setBackgroundColor(dominantSwatch.rgb)
                } else {
                    imageView.setBackgroundColor(lightVibrantSwatch!!.rgb)
                }
            })
            return false
        }
    }) 

    .into(imageView)

However, through the above code, I will load the image directly (imagview will display white before the image is loaded successfully), without filling it with solid color first.

I think maybe glide is loading too fast?

Is there any way to ensure that my picture is filled with solid color for a period of time, such as 500ms?



Solution 1:[1]

use Palette.from(resource!!.toBitmap) this will force and rap the resources to bitmap. but firstly you will need to use let on the resources variable so that resources doe's not give you issues

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Seotsa Abram Makaota