Glide for image loading and caching

Alex Chen Jan 2026
2 tabs
package com.example.myapp.utils

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.example.myapp.R

object ImageLoader {

    fun loadImage(
        context: Context,
        url: String?,
        imageView: ImageView,
        placeholder: Int = R.drawable.placeholder,
        error: Int = R.drawable.error_image
    ) {
        Glide.with(context)
            .load(url)
            .placeholder(placeholder)
            .error(error)
            .transition(DrawableTransitionOptions.withCrossFade())
            .into(imageView)
    }

    fun loadCircularImage(
        context: Context,
        url: String?,
        imageView: ImageView
    ) {
        Glide.with(context)
            .load(url)
            .apply(RequestOptions.circleCropTransform())
            .placeholder(R.drawable.placeholder_avatar)
            .error(R.drawable.default_avatar)
            .into(imageView)
    }

    fun loadRoundedImage(
        context: Context,
        url: String?,
        imageView: ImageView,
        cornerRadius: Int = 16
    ) {
        val options = RequestOptions()
            .transform(CenterCrop(), RoundedCorners(cornerRadius))

        Glide.with(context)
            .load(url)
            .apply(options)
            .into(imageView)
    }

    fun loadThumbnail(
        context: Context,
        url: String?,
        imageView: ImageView
    ) {
        Glide.with(context)
            .load(url)
            .thumbnail(0.1f)  // Load 10% quality first
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView)
    }

    fun preloadImage(context: Context, url: String) {
        Glide.with(context)
            .load(url)
            .preload()
    }

    fun loadImageWithCallback(
        context: Context,
        url: String,
        onSuccess: (Drawable) -> Unit,
        onError: () -> Unit = {}
    ) {
        Glide.with(context)
            .load(url)
            .into(object : CustomTarget<Drawable>() {
                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable>?
                ) {
                    onSuccess(resource)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    // Clean up
                }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    onError()
                }
            })
    }

    fun clearCache(context: Context) {
        Glide.get(context).clearMemory()
        // Clear disk cache on background thread
        Thread {
            Glide.get(context).clearDiskCache()
        }.start()
    }
}
2 files · kotlin Explain with highlit

Glide efficiently loads and caches images from network, disk, or resources. I use Glide.with(context).load(url).into(imageView) for basic loading. Transformations apply effects—centerCrop(), circleCrop(), RoundedCorners(). Placeholders show during loading, error images display on failure. RequestOptions configure size, format, caching. Glide automatically handles lifecycle, cancelling requests when views detach. Custom targets enable non-View loading. Generated API with annotations provides type-safe configuration. Preloading warms cache before display. Glide supports GIFs, SVGs, and video thumbnails. It's optimized for RecyclerView scrolling. The library reduces boilerplate and handles memory management automatically.