Load Media With Coil??
What Is Coil ???
An image-loading library for Android based on Kotlin Coroutines. Let us load PNG, JPG, Gif , and SVG with less code
Why Coil ?
Coil Is Fast??, provides high-performance, Lightweight, Modern, and Easy to use. Coil is Kotlin-first and uses modern libraries including Coroutines, and OkHttp.
with this example we will use the Extension Function it will let us customize the normal ImageView Class and make it able to load all types of images includes, SVG, and gifs.
//coil
implementation("io.coil-kt:coil:2.4.0")
implementation("io.coil-kt:coil-svg:2.5.0")
implementation("io.coil-kt:coil-gif:2.5.0")
2. Okay let's start Creating file name it an Extension.kt
3. Create a function like this line
领英推荐
ImageView.loadImageUrl(url: String? ){ }
4. create val coil Image loader then add .components{} to tell coil how to load SVG, and GIF images
5. add these components with SvgDecoder.Factory() , and GifDecoder.Factory() to be able to load svg and gif.
val imageLoader = ImageLoader.Builder(context)
.components {
add(SvgDecoder.Factory())
add(GifDecoder.Factory())
}
.build()
6. then make a new request and set some image attributes.
val request = ImageRequest.Builder(context)
.crossfade(true)
.crossfade(400)
.placeholder(R.drawable.ic_image_placeholder)
.error(R.drawable.ic_error)
.data(url)
.target(this)
.build()
imageLoader.enqueue(request)
7. pase the request to imageLoader.enqueue() to load image asynchronously.
this is a like of example on GitHub
check, fork , clone, and put a star if you like it