preface

  • Coil vs Picasso vs Glide: Get Ready… Go!
  • Original address: proandroiddev.com/coil-vs-pic…
  • Miguel Angel Ruiz Lopez
  • Translator: hi – DHL
  • This paper has been included in the warehouse Technical-Article-Translation

What are the advantages and disadvantages of Coil, a new image loading library, compared with Glide and Picasso, and its prospects for the future? Let’s start with what Coil is.

Coil, Kotlin’s first image-loading library, comes from the Instacart team and has an update on it.

  • R8: Fully compatible with R8, there is no need to add any confounding rules related to Coil.
  • Fast: Coil has a number of optimizations, including memory and disk caching, sampling of in-memory images, reuse of bitmaps, automatic pause/cancel requests, and more.
  • Lightweight: Coil adds 2000 methods to your APK (for applications that already use OkHttp and coroutines), which is comparable to Picasso and significantly less than Glide and Fresco.
  • Easy to Use: Coil takes advantage of Kotlin’s language features to reduce the sample code.
  • Modern: With a number of advanced features such as coroutines, OkHttp, and androidX Lifecycle tracking, Coil is currently the only library that supports androidX Lifecycle.

Through this passage you will learn the following, which will be answered in the thinking section of the translator

  • How does Kotlin exchange two variables with one line of code?
  • Compare Coil with Glide and Picasso, what are their advantages and disadvantages?
  • Kotlin is the preferred language for Android development. How do we choose Coil, Glide, and Picasso?
  • How to use Coil in a project?
  • How can Coil, Glide and Picasso compete?
  • What is the dynamic image sampling of Coil?

Next presentation, the number in the table it can be difficult to understand, but in order to better understand, in the last part, in bar chart clearly shows the performance of each library, in the translator thinking part will be more in-depth analysis of the three images load performance of the library, please read patiently a few times, should be able to learn many skills.

The translation

Coil, the new kid on the block, is growing in popularity, but why is it attracting so much attention? In today’s mainstream image-loading library environment, Coil is a light weight because it uses other libraries (coroutines, Okhttp) that many Android developers already include in their projects.

When I first saw the library, I thought these were nice improvements, but I was curious to see how it would benefit compared to other major image-loading libraries. The main purpose of this article is to analyze the performance of Coil, so let’s compare Coil, Glide, and Picasso.

How do we measure

To find out how well each of these libraries performs, we implemented an application using each of them, a simple application that downloads 10 images and displays them in a grid layout, as shown in the figure below.

All images are visible at the same time and are requested almost at the same time, so they can be considered parallel loading.

  • Test how long each image takes to load: Images downloaded ten times are averaged for more accurate data.
  • Calculate how long it takes to load a list of images: This number is important because images are loaded in parallel, so you can’t infer from a single image. This test has also been done ten times and will be averaged.

Another important point to test is the time it takes to load images for the first time and load them from the cache, which has been tested several times to cover the scenario above.

Download from the Internet

We start with the first scenario, when the cache is empty, downloading images from the network.

Glide

In the table below, you can see how long it takes to download images from the network when the cache is empty. Note that these times are averages after 10 tests.

The table below shows how long it took to load the complete list of images, along with the average time.

Picasso

Picasso’s test is the same as Glide’s, averaging the time it takes to download an image from the network when the cache is empty about 10 times.

And the time it took to load the full list of images, and the average time.

Coil

Now let’s take a look at the average time it took the protagonist Coil, Picasso and Glide to download from the Internet about 10 times when the cache was empty.

And the time it took to load the full list of images, and the average time.

Load from cache

Now for another scenario test, how long it takes to load an image when the cache is not empty.

Glide

The time taken to load an image from the cache is shown in the table below.

The time it took to load the full list of images from the cache, and the average time.

Picasso

The test case takes the same time as Glide to load the image from the cache, as shown in the table below.

And the time it took to load the full list of images from the cache, as well as the average time.

Coil

Finally, let’s take a look at the time Coil takes to load images from the cache, as shown in the table below.

The same time it took to load the full list of images from the cache, and the average time.

conclusion

To better understand the results we got in our test, we can see these numbers in the chart below, which reflects the results of each image downloaded from the web.

Glide fastest Picasso and Coil are almost identical.

But when we loaded it from the cache, as you can see in the image below, Glide was fastest, Coil was second, Picasso was slowest in most cases.

Another important test is the time it takes to load the full list of images. These numbers are important because this is how long the user has been waiting to see the full list of images. Glide was the fastest when images were loaded from the network, followed by Picasso and Coil the slowest.

The results of loading from the cache are different. Glide and Coil are almost identical; Picasso is the slowest.

Several conclusions can be drawn from these figures:

  • There are many scenarios to test, such as downloading large images, resizing images to fit containers, and so on. So I can’t say that the results might have been much different in other cases.
  • As you can see, statistics is a science with a lot of data, so 10 tests per scenario is not enough to specify which library is best, but we can get a rough idea of performance.
  • Glide seems to be faster in most cases, but generally not in large numbers. If you need to execute well, or if you are downloading a lot of images, this could be very useful for you. Also, the results of these tests may change if we use large images.
  • The Coil is a rookie image-loading library that is likely to improve significantly in the future, but for now it is only the result of comparing it with mature image-loading libraries.

The translator think

The author makes a comprehensive test of Coil, Glide and Picasso from the following scenarios.

  • Average time to download images from the network when the cache is empty.

    • The time it takes to download an image from the network.

    Result: Glide fastest Picasso and Coil are almost the same.

    • The time it took to load the complete list of images, and the average time.

    Result: Glide was the fastest, Picasso was second, Coil was slowest.

  • The average time to load an image from the cache when the cache is not empty.

    • The time taken to load an image from the cache.

    Result: Glide the fastest, Coil the second, Picasso the slowest.

    • The time it took to load the complete list of images, and the average time.

    Result: Glide and Coil are almost identical, Picasso being the slowest.

The selection of the image loading library is one of the most important parts of our application. Based on the above results, if your application does not use images heavily, I think it is better to use Coil for the following reasons:

  • Similar to Glide and Fresco, Coil supports bitmap pooling, a technique for reusing bitmap objects that are no longer in use. This can significantly improve memory performance (especially on pre-Oreo devices), but it imposes some API limitations.
  • Coil was developed based on Kotlin and designed for Kotlin to use, so the code is generally cleaner and cleaner.
  • Built for Kotlin, the Android language of choice, Coil is sure to shine in the future.
  • Moving from Glide and Picasso to Coil is very easy and the API is very similar.
  • Coil supports androidX lifecycle tracking and is currently the only web image loading library that supports androidX lifecycle.
  • Coil supports dynamic image sampling, assuming there is a 500×500 image locally, we will use a 100×100 image as a placeholder when reading a 500×500 image from disk.

If you have an image-based application with a large number of images, and the speed at which images load is one of the core metrics of your application, Coil is not the best option right now.

Coil includes features used by image loading libraries such as Glide and Picasso, as well as dynamic image sampling.

Dynamic picture sampling

For more information on image sampling, visit Coil, which briefly explains that if you have a 500×500 image locally, when loading a 500×500 image from disk, the 100×100 image will be used as a placeholder and will not be fully displayed until the loading is complete. An official GIF shows the process below.

This fades animation is visually comfortable, and the placeholder is set on the main thread to prevent the white flash when the ImageView is empty. Let’s see how to use Coil? How can Coil, Glide and Picasso compete?

How to Use the Coil

Add Coil dependency

Implementation "IO. Coil - kt: coil: 0.11.0"Copy the code

Add the following code to the build.gradle file in the App Moudule

KotlinOptions {jvmTarget = "1.8"}Copy the code

Call the code you need in your project, and here’s a summary of how Coil can be used

// Use Kotlin's advanced feature extension, Call easier imageView. Load (" https://www.example.com/image.jpg ") {crossfade (true)} the inline fun imageView. Load (uri: String?, imageLoader: ImageLoader = Coil.imageLoader(applicationContext), builder: LoadRequestBuilder.() -> Unit = {} ): RequestDisposable {return imageloader.load (context, uri) {target(this@load) builder()}} String,HttpUrl, Bitmap, Drawable, DrawableId imageView.load(R.drawable.ic_launcher_background) imageView.load(File("/path/to/image.jpg")) imageView.load("content://com.android.externalstorage/image.jpg") // ...... // Coil provides four conversions: Blur, round cut, Gray and rounded imageView. Load (" https://www.example.com/image.jpg ") {crossfade (true) Placeholder (r.lawable.ic_launcher_background) double (CircleCropTransformation()) ImageLoader1 = corely.imageloader (applicationContext) // You can create or call a third-party library (Koin) to inject your own instance. Val imageLoader2 = ImageLoader(applicationContext) // In some cases, Need a callback var remote download and then request = LoadRequest. Builder (applicationContext). Data (" https://www.example.com/image.jpg "). The target { drawable -> // Handle the successful result. } .build() imageLoader2.execute(request)Copy the code

Coil, Glide and Picasso go head to head

The Coil is based on Kotlin and, naturally, has Kotlin’s advanced functions, making it much easier to use than Glide or Picasso.

The basic use

// Coil - Using Kotlin's advanced feature extension, Imageview.load (url) // Glide Glide. With (context).load(url).into(imageView) // Picasso Pica.get () .load(url) .into(imageView)Copy the code

A background thread

/ / Coil: Val imageLoader = corely.imageloader (context) val request = getrequest.builder (context).data(url).size(width, Build () val drawable = imageLoader. Execute (request). Drawable // Glide: Val drawable = Glide.with(context).load(url).submit(width, height).get() // Picasso: Val drawable = picas.get ().load(url).resize(width, height).get()Copy the code

Automatic detection of scaleType

ScaleType = imageView.scaletype. FIT_CENTER // Coil: ScaleType imageView.load(url) {placeholder(placeholder)} // Glide Glide. With (context).load(url) .placeholder(placeholder) .fitCenter() .into(imageView) // Picasso Picasso.get() .load(url) .placeholder(placeholder) .fit() .into(imageView)Copy the code

Coil of actual combat

Jetpack project PokemonGo is a small App project based on MVVM architecture and Repository design pattern development, involving technology: Paging3 (Network + DB), Dagger Hilt, App Startup, DataBinding, Room, Motionlayout, Kotlin Flow, Coil, etc.

In this project, Coil is used to load the image. You can download it to experience it.

Warehouse address: https://github.com/hi-dhl/PokemonGo

Kotlin tip

How to exchange two variables in one line of code? Let’s start by reviewing the JAVA approach

int a = 1; int b = 2; // JAVA - intermediate variable int temp = a; a = b; b = temp; System.out.println("a = "+a +" b = "+b); // a = 2 b = 1; b = a - b; a = a - b; System.out.println("a = " + a + " b = " + b); // a = 2 b = 1 // a = a ^ b; b = a ^ b; a = a ^ b; System.out.println("a = " + a + " b = " + b); // a = 2 b = 1 // Kotlin a = b.also { b = a } println("a = ${a} b = ${b}") // a = 2 b = 1Copy the code

This is the end of the full text, if there is a helpful point like is the biggest encouragement to me!

reference

  • proandroiddev.com/coil……

conclusion

Committed to sharing a series of Android system source code, reverse analysis, algorithm, translation related articles, is currently translating a series of European and American selected articles, please continue to pay attention to, in addition to translation and thinking about each European and American article, if it is helpful to you, please help me a like, thank you!! Looking forward to growing up with you.

Plan to establish a most complete and latest AndroidX Jetpack related components of the actual combat project and related components of the principle of analysis articles, currently has included App Startup, Paging3, Hilt and so on, is gradually adding other Jetpack new members, the warehouse continues to update, Check it out: AndroidX-Jetpack-Practice, please give me a thumbs up if this repository is helpful to you, and I will continue to complete more project practices for new members of Jetpack.

algorithm

Since LeetCode has a large question bank, hundreds of questions can be selected for each category. Due to the limited energy of each person, it is impossible to brush all the questions. Therefore, I sorted the questions according to the classic types and the difficulty of the questions

  • Data structures: arrays, stacks, queues, strings, linked lists, trees…
  • Algorithms: Search algorithm, search algorithm, bit operation, sorting, mathematics,…

Each problem will be implemented in Java and Kotlin, and each problem has a solution idea. If you like algorithms and LeetCode like me, you can pay attention to my Solution of LeetCode problem on GitHub: Leetcode-Solutions-with-Java-And-Kotlin, come to learn together And look forward to growing with you

Android 10 source code series

I’m writing a series of Android 10 source code analysis articles. Knowing the system source code is not only helpful in analyzing problems, but also very helpful in the interview process. If you like to study Android source code as MUCH as I do, You can follow my Android10-source-Analysis on GitHub, and all articles will be synchronized to this repository

  • How is APK generated
  • APK installation process
  • 0xA03 Android 10 source code analysis: APK loading process of resource loading
  • Android 10 source code: APK
  • Dialog loading and drawing process and use in Kotlin, DataBinding
  • WindowManager View binding and architecture
  • 0xA07 Android 10 source code analysis: Window type and 3d view hierarchy analysis
  • More and more

Android Apps

  • How to get video screenshots efficiently
  • How to package Kotlin + Android Databinding in your project
  • Bye-bye buildSrc, embrace Composing builds for faster Android builds

Select a translation

  • [Google engineers] just released a new Fragment feature, “New ways to transfer Data between Fragments” and source code analysis
  • How does FragmentFactory elegantly use Koin and partial source code analysis
  • [2.4K Start] Drop Dagger to Koin
  • [5K +] Kotlin’s performance optimization stuff
  • Decrypt RxJava’s exception handling mechanism

Tool series

  • Shortcuts to AndroidStudio that few people know
  • Shortcuts to AndroidStudio that few people know
  • All you need to know about ADB commands
  • 10 minutes introduction to Shell scripting

The reverse series

  • Dynamically debug APP based on Smali file Android Studio
  • The Android Device Monitor tool cannot be found in Android Studio 3.2