Load Media With Coil??

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.

  1. Okay let's start add dependencies

    //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

Hassan-Elsayed-Ammer/Coil_Media_Loading: coil media loading [image, gif, svg]

check, fork , clone, and put a star if you like it

要查看或添加评论,请登录

社区洞察

其他会员也浏览了