<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kudzai</title>
    <description>The latest articles on DEV Community by Kudzai (@kudziechase).</description>
    <link>https://dev.to/kudziechase</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F247472%2F35cc7874-f7bf-4d97-8b2a-c823533da97d.jpg</url>
      <title>DEV Community: Kudzai</title>
      <link>https://dev.to/kudziechase</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kudziechase"/>
    <language>en</language>
    <item>
      <title>Taking a look at Coil. An image loading library for Kotlin on Android.</title>
      <dc:creator>Kudzai</dc:creator>
      <pubDate>Thu, 02 Jan 2020 16:11:45 +0000</pubDate>
      <link>https://dev.to/kudziechase/taking-a-look-at-coil-an-image-loading-library-for-kotlin-on-android-1ndp</link>
      <guid>https://dev.to/kudziechase/taking-a-look-at-coil-an-image-loading-library-for-kotlin-on-android-1ndp</guid>
      <description>&lt;p&gt;Happy New Year everyone! If you're reading this, it probably means that its time to get back to the code editor. I recently discovered Coil, an image loading library, I thought I would explore it a little bit and share what I found. &lt;em&gt;Ehem&lt;/em&gt;,&lt;/p&gt;

&lt;h1&gt;
  
  
  Coil: What is it?
&lt;/h1&gt;

&lt;p&gt;Well, according to their &lt;a href="https://coil-kt.github.io/coil/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; Coil stands for &lt;strong&gt;Co&lt;/strong&gt;routine &lt;strong&gt;I&lt;/strong&gt;mage &lt;strong&gt;L&lt;/strong&gt;oader and it is an image loading library backed by Kotlin Coroutines. If you aren't familiar with Kotlin Coroutines, then fear not, these are just lightweight threads. Also knowledge of Coroutines isn't required to use this library so I will not be diving into the intricate details of Kotlin Coroutines.&lt;/p&gt;

&lt;h2&gt;
  
  
  But do we really need another image loading library?
&lt;/h2&gt;

&lt;p&gt;You are probably wondering if we really need another image loading library in a world full of Glide and Picasso or Universal Image Loader -  &lt;em&gt;for those of you who have been developing on the Android platform for a while now&lt;/em&gt;😉. However two things caught my attention with regards to Coil and that is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It is smaller in size compared to Glide, which was my image loading library of choice. And,&lt;/li&gt;
&lt;li&gt;It is surprisingly very easy to use. You shall see further down in this article, that the library is very idiomatic, which is what Kotlin is all about and that is awesome. &lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Okay I'm sold, what next?
&lt;/h1&gt;

&lt;p&gt;In order to use Coil in your project you need to have your project already using AndroidX. It must also compile with SDK level 29 as well as use Java 8. &lt;/p&gt;

&lt;p&gt;If you satisfy the above per-requisites then you need to add in the following line to your application level dependencies in your &lt;code&gt;build.gradle&lt;/code&gt; file before you can use the library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"io.coil-kt:coil:0.9.1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will include the default artifact with the &lt;code&gt;Coil&lt;/code&gt; singleton. Just like that, you are ready to start easier and faster Image Loading in your Android applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Loading an Image
&lt;/h2&gt;

&lt;p&gt;To load an image to an &lt;code&gt;ImageView&lt;/code&gt;, Coil offers a nifty extension function called &lt;code&gt;load()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Below is a code snippet of the library in action:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Loading from a URL&lt;/span&gt;
&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://via.placeholder.com/600/92c952"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;//Loading from an image drawable&lt;/span&gt;
&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drawable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;some_image_placeholder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Loading from a file&lt;/span&gt;
&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/path/to/some_image_placeholder.png"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Walla! It's really that simple. &lt;em&gt;You can't make this stuff up!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Under the hood, the &lt;code&gt;load()&lt;/code&gt; function does a lot of things. Take for instance loading from a URL: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Coil creates a &lt;code&gt;RequestDisposable&lt;/code&gt; object, which is basically an object responsible for connecting to the network.&lt;/li&gt;
&lt;li&gt;The image specified in the URL is requested for over the network and the result is loaded onto a &lt;code&gt;target&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In this case the instance of the &lt;code&gt;ImageView&lt;/code&gt;is the intended &lt;code&gt;target&lt;/code&gt;(&lt;em&gt;and hence the beauty of extension functions&lt;/em&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Let's explore a little more...
&lt;/h1&gt;

&lt;p&gt;At this point, you are probably wondering how to make this library do certain things like configure a placeholder or maybe apply a transformation to circle crop your image. &lt;/p&gt;

&lt;p&gt;Well its really easy. You just add in your configurations to a trailing lambda after the &lt;code&gt;load()&lt;/code&gt; extension function. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Observe:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Applying image loading with configurations&lt;/span&gt;
&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://via.placeholder.com/600/92c952"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;crossfade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drawable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;placeholder_image&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;transformations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CircleCropTransformation&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Very simple!.&lt;/p&gt;

&lt;p&gt;As if that wasn't enough, Coil supports loading to custom targets.&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;load()&lt;/code&gt; extension function from the &lt;code&gt;Coil&lt;/code&gt; singleton, you just have something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Custom targets&lt;/span&gt;
&lt;span class="nc"&gt;Coil&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"https://via.placeholder.com/600/92c952"&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nf"&gt;target&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;drawable&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
        &lt;span class="c1"&gt;//Do something with result e.g apply a filter to greyscale image.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can even listen in for specific events like &lt;code&gt;onSuccess&lt;/code&gt;, &lt;code&gt;onStart&lt;/code&gt;, &lt;code&gt;onError&lt;/code&gt; and &lt;code&gt;onCancel&lt;/code&gt;, however I will not be getting into these in this article. You can let me know if you want me to dive deeper. And I will create a &lt;strong&gt;Coil party tricks&lt;/strong&gt; 2 part article touching every aspect of the library I can find.&lt;/p&gt;

&lt;h2&gt;
  
  
  A little bit of more information on Transformations
&lt;/h2&gt;

&lt;p&gt;The Coil library provides 4 transformations out of the box namely,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;BlurTransformation&lt;/code&gt; - Applies a Gaussian Blur.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CircleCropTransformation&lt;/code&gt; - Crops and centers the image into a circle.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;GrayscaleTransformation&lt;/code&gt; - Shades the image into grayscale.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RoundedCornersTransformation&lt;/code&gt; - Crops the image to fit the target's dimensions and rounds the corners of the image.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other cool things to note 😎
&lt;/h2&gt;

&lt;p&gt;There are a few cool other things to note about the Coil library.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Its support for extensions is massive and this allows for developers to support more file types in their projects to suit their needs.&lt;/li&gt;
&lt;li&gt;Its has features which makes testing easier. You can basically mock instances of the &lt;code&gt;ImageLoader&lt;/code&gt;object as it is an interface. &lt;/li&gt;
&lt;li&gt;No annotation processing! And this is a big one. This means faster builds in your project in case you were using Glide, you know how important this is. They use Kotlin's extension functions feature.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  In Conclusion
&lt;/h1&gt;

&lt;p&gt;Coil is a pretty neat image loading library for Android. I am in no ways associated with Instacart however I am an advocate for cleaner code and I have always been lazy. I will 💜 anything that makes my life easier and Coil is that thing, atleast for image loading lol!&lt;/p&gt;

&lt;p&gt;Please go forth and give it a try, &lt;em&gt;maybe even migrate from your current Image Loading library.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you would like to know more I have included a few links that may be use to you below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://tech.instacart.com/introducing-coil-kotlin-first-image-loading-on-android-f0fdc7a2a99e" rel="noopener noreferrer"&gt;Introducing Coil: Kotlin-first image loading on Android&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://coil-kt.github.io/coil/" rel="noopener noreferrer"&gt;Coil Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/coil-kt/coil" rel="noopener noreferrer"&gt;Coil Github Project&lt;/a&gt; - Browse through the code. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4b38m3x90cy238xssjbv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4b38m3x90cy238xssjbv.gif" alt="That's all folks" width="400" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to follow me on &lt;a href="https://twitter.com/KudzieChase" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;. I'm always up for a discussion.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>java</category>
    </item>
  </channel>
</rss>
