<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cyril Mottier on Medium]]></title>
        <description><![CDATA[Stories by Cyril Mottier on Medium]]></description>
        <link>https://medium.com/@cyrilmottier?source=rss-bf832913ceeb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xSKRBnmVlT7bcGnG-b17lA.jpeg</url>
            <title>Stories by Cyril Mottier on Medium</title>
            <link>https://medium.com/@cyrilmottier?source=rss-bf832913ceeb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:33:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cyrilmottier/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing Custom Drawables — part 1]]></title>
            <link>https://medium.com/zenlyapp/implementing-custom-drawables-part-1-5530a98cefc9?source=rss-bf832913ceeb------2</link>
            <guid isPermaLink="false">https://medium.com/p/5530a98cefc9</guid>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Cyril Mottier]]></dc:creator>
            <pubDate>Tue, 26 Jun 2018 13:18:44 GMT</pubDate>
            <atom:updated>2018-06-27T10:43:24.513Z</atom:updated>
            <content:encoded><![CDATA[<p>Android has always offered several ways and layers to draw things on screen, one of which is Drawable. As its name suggests, Drawable is a general abstraction of “something that can be drawn”. In particular, it can be considered as an entity that can be drawn onto a Canvas— the vessel for drawing commands on Android. As such, some might say it makes it similar to View. Indeed, both provide ways to render on a Canvas. However, Drawable doesn’t deal with things like touch event dispatching, save state, measure and layout, etc. As it only draws things it makes it a much more suitable and light-weight option when it only comes to draw on Android.</p><p>Being a very demanding app in term of rendering content, the Zenly app makes an extensive use of Drawables. It obviously relies a lot on PNG/JPG images (i.e. BitmapDrawable and NinePatchDrawable). However, in some cases, framework-provided Drawables are not enough or don’t exactly match our needs. In these cases, we generally have to create our own rendering code. Implementing custom Drawables is one of our prefered way to do so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c0hsn9FSak7sncCtaHXPLQ.jpeg" /><figcaption>A corridor in the Zenly office.</figcaption></figure><p>At Zenly, we love colors and 🌈. As a consequence, it’s completely logical for us to use bold colors and include rainbow stripes in the app design! This is exemplified in a feature called “Best Friends” which is unlocked once you have enough friends on Zenly. “Best Friends” let’s you access some nice information about your friends: your favorite workmate or lunchmate, time spent together in the last 7 days, pals you haven’t seen for a long time, etc. In order to make this informative screen look cool, the header features a striped rainbow background. I encourage you to test this app section for real (yep… there is more 😉) but here is what it looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*VPZaNX6hw6UcHamIgxWCmQ.png" /><figcaption>Who are your best friends!?!</figcaption></figure><p>As shown in the image above, the header background is made of several colorful stripes. In practice, vertical stripes are tilted 30° clockwise and are repeated to fully fill the header bounds.</p><h3>The original implementation</h3><p>The very first naive implementation of this “rainbow stripes” effect consisted of replicating a pattern image to fill the entire area. In practice, we had an image name rainbow_stripes_pattern exported in the three densities supported by the app: hdpi, xhdpi and xxhdpi. The asset was intelligently sliced so that it could act as a repeatable pattern. Indeed, as shown below, you can put the same image (represented in translucent opacity) in any direction of a fixed image (shown completely opaque), it will always give the impression you are expanding the stripes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*laeoNOG57RWXwVtHwC7MLg.png" /><figcaption>Let’s repeat tiles over and over again…</figcaption></figure><p>In app, this “repeat” effect was implemented creating a new rainbow_stripes BitmapDrawable in XML with tileMode set to repeat. This special tile mode simply informs the rendering pipeline that the image should be repeated on both the X and Y axis rather than stretched to fill the Drawable bounds:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19f58b8dea056d11f05329da7ed81735/href">https://medium.com/media/19f58b8dea056d11f05329da7ed81735/href</a></iframe><p>In order to display the rainbow stripes, you simply need to set the Drawable defined above as the background of a View.</p><h3>A better implementation</h3><p>From a strict product point of view, the UI appearance was satisfying. The app was displaying infinite colorful rainbows after all! Unfortunately, although it was visually correct, the technique described above was suffering from several drawbacks and limitations:</p><ol><li>Exporting and updating the pattern image was a nightmare. Stripes beeing tilted by 30° made it impossible to have pixel-perfect pattern size. Designers had to either tweak the angle or trick users by ensuring misalignment was only visible at a sub-pixel level.</li><li>In order to have smooth and sharp-edged stripes, the APK contained the same image in several densities making it larger in size. Fortunately, image compression and the recent introduction of dynamic delevery reduces the impact on the end users.</li><li>Loading the image at runtime was quite heavy in term of memory resources. Indeed, the image being 360x624dp, loading it into memory was taking about 3.5MB in xhdpi (and up to 8MB in xxhdpi). Being that Zenly is quite intensive in term of memory usage, it didn’t feel right to take up so much memory for an easter-egg.</li></ol><p>Pretty logically, the alternative is to render the rainbow stripes dynamically (i.e. at runtime). A possible implementation would be to draw several rectangles on a 30° rotated Canvas. While this solution would probably work, it looks pretty complex. A better and simpler solution is to consider the stripes as a gradient whose colors and stops are determined in a way in which the switch between two colors is instant.</p><p>The Android framework offers several ways to render a gradient. The most obvious way is GradientDrawable. However, its API is pretty high level and only allows special types of gradient. For instance, it doesn’t let you choose an angle which is not multiple of 45° nor the color stops. As a consequence, GradientDrawable is not a suitable option in our case.</p><p>The correct way to draw such a gradient, is to use the low-level API that powers GradientDrawable: LinearGradient. LinearGradient is a Shader you can set on a Paint. In general, Paint describes a single color, a single font, etc. Attaching Shaders to a Paint allow passing information about how to draw multiple colors at once.</p><p>The API of LinearGradient is pretty simple as the class only exposes 2 constructors. The first constructor is a convenience initializer as it only accepts 2 colors. The second one is the most generic one and accepts an array of colors as well as an array of stops/positions. You may have guessed it, we will use the most generic one in this case.</p><p>The LinearGradient generic constructor requires several arguments as input. Indeed, a gradient is generally defined by 4 main values: two 2D coordinates defining the gradient line and two arrays defining, as described previously, the colors to be distributed along the gradient line and the relative position (in the [0, 1] range) of each corresponding color in the colors array. A final parameter defines the “tile mode” (cf <a href="https://developer.android.com/reference/android/graphics/Shader.TileMode">Shader.TileMode</a>) and describes how the rendering pipeline should deal with shaders not filling the entire bounds (e.g. clamp, mirror or repeat).</p><p>In order to build our LinearGradient, we need to look at the original asset. Thanks to some basic math and a little trigonometry, we can easily compute the start and end coordinates of our gradient line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vc13WR42faoNv6b-2F7ttA.png" /><figcaption>Having fun with trigonometry!</figcaption></figure><p>First of all, let’s start initializing some constants:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/71c248e0ceacf3d85aaf638f44741221/href">https://medium.com/media/71c248e0ceacf3d85aaf638f44741221/href</a></iframe><p>The first step consists on computing the colors and stops:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b819167fc331f4021978c5d9a784d88/href">https://medium.com/media/1b819167fc331f4021978c5d9a784d88/href</a></iframe><p>Once both the colors and stops arrays are initialized, you can easy build a LinearGradient and expose it thanks to a Drawable. This makes it easy to integrate in your app (as the background of a View for instance). Please keep in mind, the following Drawable doesn’t respect all of the Drawable APIs. For instance, there is no support for alpha, color filters, constant state, etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01502cdaf0cc40e4931b9dabd24ae54a/href">https://medium.com/media/01502cdaf0cc40e4931b9dabd24ae54a/href</a></iframe><p>Using this newly created custom Drawable as the background of a fullscreen View works as expected: rainbow stripes are correctly rendered and are repeated infinitely to fully fill the bounds of the View. There’s a small glitch though. Indeed, if you look closely at the junction between two colors, you’ll notice a “darker” line as shown magnified on the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ysYfhDRQ2MAtr6jcr5qv4w.png" /></figure><p>To be totally honest, I couldn’t find the exact reason behind this glitch when looking at AOSP source code. Vector rendering software such as Affinity Designer or Sketch don’t have the issue. My only assumption here is the Android rendering pipeline overlaps two colors at stop points forming a darker thin line.</p><p>In order to workaround it, you can add/subtract a small value to stop points. Let’s call the small value <em>epsilon</em>. If we consider the frontier between color <em>N</em> and color <em>N+1</em>, we end up having color <em>N</em> ending stop reduced by <em>epsilon</em> and color <em>N+1</em> starting stop augmented by <em>epsilon</em>. The code below shows the modified code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4617169f4dc07631eea1d2e07c0ee5d/href">https://medium.com/media/e4617169f4dc07631eea1d2e07c0ee5d/href</a></iframe><p>The final rendering looks exactly as expected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wQJfXnsowcwErldfqLJ_zw.png" /></figure><h3>Conclusion</h3><p>And we’re done! Building custom Drawables on Android can be a huge win in some cases. For instance, it can help reduce the memory footprint (disk and RAM). In most cases, the Android UI toolkit APIs provide all of the necessary concepts to render things created by your designers on tools like Affinity Designer or Sketch. The only tricky (but fun 🤓) part is generally to find a way to render these nice UIs in code.<br> <br>If you like this kind of UI tricks and optimizations, my team at Zenly is hiring. We’re looking for talented iOS and Android engineers. Come say 👋 on <a href="https://twitter.com/cyrilmottier">Twitter</a>, my DMs are open.</p><p><strong>Edit</strong> (<em>Jun 27, 2018)</em>: <a href="https://medium.com/u/22c02a30ae04">Nick Butcher</a> created a similar effect using only XML code. The implementation relies on VectorDrawable. You can have a look at his tweet for more details on the implementation:</p><h3>Nick Butcher on Twitter</h3><p>You can also create a similar effect using a VectorDrawable. https://t.co/gXF5CCpxm0</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5530a98cefc9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/zenlyapp/implementing-custom-drawables-part-1-5530a98cefc9">Implementing Custom Drawables — part 1</a> was originally published in <a href="https://medium.com/zenlyapp">zenly blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>