<?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 Abhijeet Singh Chauhan on Medium]]></title>
        <description><![CDATA[Stories by Abhijeet Singh Chauhan on Medium]]></description>
        <link>https://medium.com/@chauhanabhi1245?source=rss-a4af4a4c47ba------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7OD2oGiQLRcG4aO5fo-skg.jpeg</url>
            <title>Stories by Abhijeet Singh Chauhan on Medium</title>
            <link>https://medium.com/@chauhanabhi1245?source=rss-a4af4a4c47ba------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 11:17:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chauhanabhi1245/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[Building custom shapes with Canvas in Jetpack Compose: Practical Guide]]></title>
            <link>https://medium.com/@chauhanabhi1245/building-custom-shapes-with-canvas-in-jetpack-compose-practical-guide-42faefe2654e?source=rss-a4af4a4c47ba------2</link>
            <guid isPermaLink="false">https://medium.com/p/42faefe2654e</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[mobile]]></category>
            <dc:creator><![CDATA[Abhijeet Singh Chauhan]]></dc:creator>
            <pubDate>Sun, 13 Aug 2023 11:43:25 GMT</pubDate>
            <atom:updated>2023-08-13T11:43:25.710Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B3h3l9F9OTw3s9vhiJ2Adg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@zekedrone?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Martin Sanchez</a> on <a href="https://unsplash.com/s/photos/canvas-lines?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>Before getting drenched in the universe of <strong>Compose</strong>, we should revisit what Compose is.</p><blockquote>Jetpack Compose is Android’s modern toolkit for building native UI. It simplifies and accelerates UI development on Android bringing your apps to life with less code, powerful tools, and intuitive Kotlin APIs. It makes building Android UI faster and easier.</blockquote><p>Don’t get overwhelmed by all those fancy keywords. You are going to witness each of those keywords in action right away.</p><p>This blog aims to give a practical example to unfold the simplicity and ease of canvas drawing.</p><h3>Challenge:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D6SlibSbfMh2ZO-B3Zvq-A.png" /></figure><p>The design above aims at allowing travelers to select seats for their journey irrespective of the mode of transport <em>(</em><strong><em>possible variations: flight ✈️, bus🚍, cruise 🛳 </em></strong><em>)</em></p><p><strong><em>Design Requirements:</em></strong><em><br>- The component can have wings (for Flights) or be wingless (for Bus)<br>- Should be </em><strong><em>vertically/horizontally</em></strong><em> scrollable if overflowing the device dimensions.<br>- All coordinates and dimensions should be configurable including wing start and end junctions.</em></p><p>Although the component may appear daunting initially, we will complete it together. Are you ready?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/0*JMC_u7XeVVrWxeBj.gif" /></figure><h3>Approach:</h3><p>The first step is to breakdown the following UI component into smaller <br>re-usable composable such as:<br>- <strong>Polygon outline</strong><br>- <strong>M*N</strong> <strong>Grid</strong> <strong>of Cells</strong> <em>{ Seat | Column Name | Row Number | Exit | Aisle }<br>We need to draw the shape, superimpose it with the grid and we are done.</em></p><h4>Component 1: Seat Map Outline Shape</h4><p>The shape outline composable needs to be flexible enough to be re-used across various transport modes such as Flight/Bus/Cruise.</p><p>It is evident from the design that the above component is simply a combination of <strong>lines</strong> drawn over a Compose <strong>Canvas</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*6bIbfaDZ-QyK4pwU.jpg" /></figure><p><strong>Step 1: Process Input</strong></p><pre>data class SeatMapCabinDetails(<br>    val wingDetails: SeatMapCabinWingDetails?, // applicable for flights only<br>    val rowsCount: Int,    // seat rows count<br>    val columnsCount: Int, // seat column counts<br>    val exitsCount: Int,   // exits if any <br>    val aislesCount: Int   // aisles in between seats<br>)<br><br>data class SeatMapCabinWingDetails(<br>    val startRow: Int,     // wing starts from this row<br>    val endRow: Int        // wing retracts on this row<br>)</pre><p>Our first goal is to transform the <br><strong><em>Input</em></strong>: <em>SeatMapCabinDetails</em> (holds cabin details)<br>To<br><strong><em>Output</em></strong>: <em>SeatMapOutlineData (contains an array of points to draw the polygon)</em></p><pre>internal data class SeatMapPoints(<br>    val x: Float, // x co-ordinate for Point<br>    val y: Float  // y co-ordinate for Point<br>)<br><br>internal data class SeatMapOutlineData(<br>    val totalCanvasWidth: Float, // canvas width taking column,exits, aisles<br>    val totalCanvasHeight: Float,// canvas height to accomodate rows<br>    val points: ArrayList&lt;SeatMapPoints&gt; // polygon points to be joined by line<br>)</pre><p>For simplicity, we can keep totalCanvasWidth and totalCanvasHeight constant. However, practically <br><em>totalCanvasWidth = (column + exit + aisle)* (item_width)<br>totalCanvasHeight = rows * item_height</em></p><p><strong><em>Wingless: <br></em></strong>Only 4 coordinates are required to draw the component without wings<br>starting from<br><em>- top-left corner with some initial padding <br>- line to the bottom left corner (based on cabin height)<br>- line from bottom-left to bottom-right (based on cabin width)<br>- line to top-right <br>- line back to top-left closing the shape</em></p><p><strong><em>With Wing:<br></em></strong>Things are a little tricky when it comes to wing angles starting from <br><em>- top-left with padding equal to one-wing width on the left<br>- top-left to left-wing start based on start wing count<br>- left-wing start to the left-wing intersection start based on the aspect ratio of the wing<br>- left-wing start to left-wing intersection end<br>- retrace back to the plane body from the left-wing intersection end<br>- move downwards to the end of the canvas using rows<br>- move towards the right based on column and seat width<br>- move upwards in a similar fashion we did the left-wing coming downwards</em></p><pre>internal fun getOutlinePoints(<br>    horizontalPaddingStart: Float,<br>    cabinWidth: Float,<br>    cabinDimensionDetails: SeatMapCabinDetails,<br>): SeatMapOutlineData {<br>    val isWingAvailable = cabinDimensionDetails.wingDetails != null<br>    val heightPadding = 44.0f // seat size with padding<br><br>    // dummy value: replace with wing-gap * LAYOUT_PADDING_MULTIPLIER<br>    val xOffset = 60.0f <br><br>    // Points array for non-wing shapes   <br>    var points = arrayListOf(<br>        // top-left to bottom-left<br>        SeatMapPoints(xOffset, totalCanvasHeight),<br>        // bottom-left to bottom-right<br>        SeatMapPoints(cabinWidth + xOffset, totalCanvasHeight),<br>        // bottom-right to top-right<br>        SeatMapPoints(cabinWidth + xOffset, 0.0f),<br>        // top-right to top-left<br>        SeatMapPoints(xOffset, 0.0f)<br>    )<br><br>    // Points array for wing shapes if available<br>    cabinDimensionDetails.wingDetails?.let { wingDetails -&gt;<br>        // number of rows till this point = wingDetails.startRow<br>        val leftWingStartCoordinate = wingDetails.startRow * heightPadding<br>        val leftWingEndCoordinate = (wingDetails.endRow + 1) * heightPadding<br>        val leftWingIntersectionStartYCoordinate =<br>            leftWingStartCoordinate + horizontalPaddingStart * WING_HEIGHT_UPPER_MULTIPLIER<br>        val leftWingIntersectionEndYCoordinate =<br>            leftWingEndCoordinate + horizontalPaddingStart * WING_HEIGHT_LOWER_MULTIPLIER<br><br>        points = arrayListOf(<br>            // top-left to left-wing start<br>            SeatMapPoints(xOffset, leftWingStartCoordinate),<br>            // left-wing start to left-wing intersection start<br>            SeatMapPoints(0f, leftWingIntersectionStartYCoordinate),<br>            // left-wing start to left-wing intersection end<br>            SeatMapPoints(0f, leftWingIntersectionEndYCoordinate),<br>            // retrace back to plane body<br>            SeatMapPoints(xOffset, leftWingEndCoordinate),<br>            // line to bottom-left<br>            SeatMapPoints(xOffset, totalCanvasHeight),<br>            // bottom-left to bottom-right<br>            SeatMapPoints(xOffset + cabinWidth, totalCanvasHeight),<br>            // bottom-right to lower right-wing end(same Y as leftWingEnd)<br>            SeatMapPoints(xOffset + cabinWidth, leftWingEndCoordinate),<br>            // lower right-wing to end of canvas<br>            SeatMapPoints(totalCanvasWidth, leftWingIntersectionEndYCoordinate),<br>            // end of canvas to right-wing start<br>            SeatMapPoints(totalCanvasWidth, leftWingIntersectionStartYCoordinate),<br>            // retrace back to plane body<br>            SeatMapPoints(xOffset + cabinWidth, leftWingStartCoordinate),<br>            // move-up <br>            SeatMapPoints(xOffset + cabinWidth, 0.0f),<br>            // move to inital point<br>            SeatMapPoints(xOffset, 0.0f)<br>        )<br>    }<br>    return SeatMapOutlineData(totalCanvasWidth, totalCanvasHeight, points)<br>}<br>// These ratios are taken from figma to keep wings realistic<br>private const val WING_HEIGHT_UPPER_MULTIPLIER = 0.49f<br>private const val WING_HEIGHT_LOWER_MULTIPLIER = 0.16f<br>const val LAYOUT_PADDING_MULTIPLIER = 0.58f</pre><p><strong>Step 2: Supply pre-computed values to the drawing function</strong></p><p>Creating a composable requires the following parameters:<br>- Total width of canvas based on wing availability and columns<br>- Total height based on the number of rows <br>- Array of (x,y) to draw lines</p><pre>@Composable<br>internal fun SeatMapOutline(<br>    modifier: Modifier = Modifier,<br>    seatMapOutlineData: SeatMapOutlineData,<br>) {<br>    // dummy value: replace with wing-gap * LAYOUT_PADDING_MULTIPLIER<br>    val xOffset = 60.0f // this is intial drawing point<br><br>    val bgColor = Color.Gray<br>    val outlineColor = Color.Black<br>    Canvas(<br>        modifier = modifier<br>            .width(seatMapOutlineData.totalCanvasWidth.dp)<br>            .height(seatMapOutlineData.totalCanvasHeight.dp),<br>        onDraw = {<br>            val path = Path().apply {<br>                reset()<br>                // move the pointer from (0,0) to (intialStartX, 0)<br>                moveTo(xOffset.dp.toPx(), 0f)<br>                // loop through list of coordinates calculated in previous step<br>                seatMapOutlineData.points.forEach { point -&gt;<br>                    lineTo(point.x.dp.toPx(), point.y.dp.toPx())<br>                }<br>                // close the path to join back to start point<br>                close()<br>            }<br>            // fill canvas with background color<br>            drawPath(<br>                path,<br>                color = bgColor,<br>                style = Fill<br>            )<br>            // draw lines with outline color<br>            drawPath(<br>                path,<br>                color = outlineColor,<br>                style = Stroke(<br>                    width = 2.0f,<br>                    cap = StrokeCap.Round<br>                )<br>            )<br>        }<br>    )<br>}</pre><p><em>Disclaimer</em>: <em>drawPath is used twice to draw a colored border and once to fill the background.</em></p><h3>Outcome</h3><p>Here is the outline drawn on compose canvas for our component, with the supplied coordinates array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*hdDZNbPmo4hsZqDtJBo-rQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*f0puMeMdzaUDn3XPT-Cmzg.png" /><figcaption>Wing and Wing-less outline shapes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/0*rzhQh_Xoricl7XyX.gif" /></figure><h3>Wrap it up 🌯</h3><p>Jetpack Compose has truly elevated the canvas drawing experience with its declarative UI approach, offering a higher level of abstraction and intuitive component creation. <br>By allowing us to focus on the desired outcome rather than implementation details, Compose enabled us to achieve cleaner and more maintainable code.</p><p>In contrast, custom view drawing on the canvas demands a deeper understanding of low-level APIs and the Android graphics system. <br>While it provides greater control and flexibility, it can be complex and time-consuming, particularly for intricate UI interactions or animations.</p><p>Given our specific use case centered around drawing components, Jetpack Compose proved to be the perfect tool, delivering exceptional ease and efficiency in achieving our goals.</p><h3>Next Steps</h3><p>Hold onto your seats because Part 2 is just around the corner! <br>Get ready to dive into another fascinating exercise using Jetpack Compose. We’ll unleash the power of Compose’s flexibility and composability to create a mind-boggling seating arrangement that adapts on the fly. <br>Stay tuned for an electrifying journey as we push the boundaries of what’s possible with Jetpack Compose in Part 2 of this practical guide!</p><p>Always up for<strong> discussing anything cool</strong>, feel <strong>free to connect<em>.</em></strong><br><strong>LinkedIn</strong>: <a href="https://www.linkedin.com/in/chauhan-abhi/">chauhan-abhi</a><br><strong>Twitter: </strong><a href="https://twitter.com/andro__abhi">andro__abhi</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42faefe2654e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>