<?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 Banchanattu on Medium]]></title>
        <description><![CDATA[Stories by Banchanattu on Medium]]></description>
        <link>https://medium.com/@banchanattu?source=rss-d60a7c2f155c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1XA6doeJP-Tt_tbE</url>
            <title>Stories by Banchanattu on Medium</title>
            <link>https://medium.com/@banchanattu?source=rss-d60a7c2f155c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 00:01:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@banchanattu/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[A Deep Dive: Mathematical look at Android Transformation]]></title>
            <link>https://banchanattu.medium.com/anatomy-of-android-transformation-c0c16abf11e3?source=rss-d60a7c2f155c------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0c16abf11e3</guid>
            <category><![CDATA[scale-rotate]]></category>
            <category><![CDATA[android-graphics]]></category>
            <category><![CDATA[transformation]]></category>
            <dc:creator><![CDATA[Banchanattu]]></dc:creator>
            <pubDate>Thu, 16 Sep 2021 15:05:29 GMT</pubDate>
            <atom:updated>2021-09-18T13:16:36.171Z</atom:updated>
            <content:encoded><![CDATA[<p>Android Graphics</p><p>In the previous section, we saw how a transformation can be defined. Now we will take a detailed look at the mathematical sense of this. This will help us to define different transformations. Also, we will prepare ourselves to get ready to implement animations using transformation. Now let us build our basic knowledge in detail.</p><h4>Transformation as a set of equations</h4><p>In two dimensional world, the equation of transformation from one rectangular coordinate to another can be written as a set of equations as below. Let X′ be the new coordinate transformed from X coordinate</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*6T7mSdouCGuyGyjJDoxb7A.png" /></figure><p>To make it more convenient to represent this in matrix form let me add one convenient dummy equation</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*ETLfQUQ13kLY0EvcxDArPg.png" /></figure><p>This can be rewritten as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*kM-lRpP5FGWHvpjZoe_n3w.png" /></figure><p>Where</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*jaln06DaRpeJ6xfjM57wmw.png" /></figure><h4>Combination of transformation</h4><p>Let me make a statement and prove it.</p><p><strong><em>Multiple transformations can be combined to form a single transformation using matrix multiplication</em></strong>.</p><p>Proof: Let X′′ be the final transformation we are looking for.</p><p>Let X′ = A.X be the transformation that takes X to X′</p><p>And let X′′ = B.X′ be the transformation that takes X′ to X′′ So, X′′=B.X′ =⇒X′′=B.A.X=⇒X′′=M.XwhereM=B.A</p><p><strong>Types of Transformation</strong></p><p>There are various linear transformations. Unity transformation, Translation, scaling, rotation, and skew are types of transformation. We can also combine these transformations. For each transformation, there is a corresponding transformation.</p><p><strong><em>1 Unity Transformation</em></strong></p><p>This transformation is not of any specific interest because there are no transformations occur. We don’t have to do any transformation at all in this case. But for the sake of completeness, we include it here.</p><p>Unity transformation equations are</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/298/1*r-hMim1m0XNSM-DNv1Ufmw.png" /></figure><p>In the matrix transformation form, it is as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*73fxIlnf6bprYtD1wy_4tw.png" /></figure><p>In the matrix form, it is X′ = M.X. M is the unit matrix shown in above equation 1.5</p><p>We can prepare this transformation matrix using the below Kotlin code</p><pre>var M: Matrix = Matrix()</pre><h4>2 Translation transformation</h4><p>This transformation moves a point to dx and dy distances from the original x, y value. It has the following form</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*T7c-VnaeYHOQpyo_YPlMgQ.png" /></figure><p>In the matrix, the transformation form is as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*z6RSEQ1gIpOBXyK4pW_oiA.png" /></figure><p>In the matrix form, it is X′ = M.X M is the unit matrix shown in above equation 1.7 We can prepare this translation using the following Kotlin code</p><pre>var M: Matrix = Matrix()</pre><pre>M.setTranslate(dx, dy)</pre><h4>3 Rotation transformation</h4><p>Rotation transformation is a rotation of (x, y) about a central point. For the sake of brevity, let us assume the point about which the rotation happening in the center of the coordinate (0, 0). As in the image below, point A is rotated about the center and moved to A′.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/566/1*l9_Aoi7ot4-McqgTO8F7HA.jpeg" /></figure><p>The OA and OA′ have the same length. Let this length be r. We can write the following equations from this setup.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*7zhxEtDWzj5s7mz7h1Nq_w.png" /></figure><p>And</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/1*uuXax-H8-xjLgnIU9aJzSA.png" /></figure><p>The equation 1.10 can be expanded as below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*vKTbYcFrPBuI_wMjaGca5g.png" /></figure><p>Using 1.8 and 1.9 this can be written as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/344/1*HZefSzuxSSkVPEeV46l9EA.png" /></figure><p>Similarly</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*EV7M8SiPA4KolGBagXjFmg.png" /></figure><p>Using 1.8 and 1.9 this can be written as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*4JawuZw2NrmSwiLRBdd-TA.png" /></figure><p>Using equations 1.13 and 1.15 we can write the matrix form as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*niFEoqj3IyydR2QP2rT18A.png" /></figure><p>In the matrix form, it is X′ = M.X. M is the matrix shown in above equation 1.16 We can prepare this translation using the following Kotlin code</p><pre>var M: Matrix = Matrix()</pre><pre>M.setRotate(degree)</pre><h4>4 Skew transformation</h4><p>A skew transformation is in the form as following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*w13WDcWZjOnGCX2hvsxMnQ.png" /></figure><p>where dy is the y directional skew; and dx is the x directional skew. Using equations 1.13 and 1.15 we can write the matrix form as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*bsHUSJ3WGT_LprAyvkdZbg.png" /></figure><p>In the matrix form, it is X′ = M.X. M is the matrix shown in above equation 1.19 We can prepare this translation using the following Kotlin code</p><pre>var M: Matrix = Matrix()</pre><pre>M.setSkew(dx, dy)</pre><p>In the forthcoming article, we will discuss how these transformations can be combined and create more useful but complex transformations. Understanding this transformation will help us to create very user-friendly animations and layouts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0c16abf11e3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Transformation an introduction]]></title>
            <link>https://banchanattu.medium.com/android-graphics-8c6c655752c6?source=rss-d60a7c2f155c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c6c655752c6</guid>
            <dc:creator><![CDATA[Banchanattu]]></dc:creator>
            <pubDate>Fri, 10 Sep 2021 02:28:46 GMT</pubDate>
            <atom:updated>2021-09-14T03:36:48.376Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android Graphics</h3><p><strong>Prerequisite: </strong><a href="https://banchanattu.medium.com/android-graphics-a65e33ef86a2"><strong>Read Andriod Graphics creating the first program</strong></a></p><p>My other <a href="https://medium.com/@banchanattu/android-graphics-a65e33ef86a2">article</a> describes how to write a basic android graphics program. The four points in the final result were corners of a square. As you might notice the X value increases as we go down direction. And Y value increases as we move in the right direction.</p><p>We are all taught in high school that X increases Upward and Y increases toward the right in a cartesian coordinate system. If we want to follow that convention we need to do some transformation of these coordinates from the Android Coordinate system to the Cartesian coordinate system.</p><p>These kinds of coordinate transformations are important when we translate real-world images or data to the Android canvas. In this section, we will discuss how to achieve it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R59nUUw10mHTwXB6tkWRLA.png" /></figure><p>How will we do it?</p><p>Android Matrix is the answer. This library implements the Affine transformation. We will learn about Affine Transformation late. To start with we will start using the affine transformation using matrix.</p><h4>Create a class named AffineTransformer</h4><p>In the android studio, create a class named AffineTransformer as the code below</p><pre>package com.example.transformation<br><br>class AffineTransformer {<br>}</pre><p>Since all transformation we are doing here depends on the size of the Activity screen let us add a default constructor that takes the basic width and height of the drawing canvas.</p><pre>package com.example.transformation<br><br>class AffineTransformer <strong>constructor(width: Int, height: Int)</strong> {<br>}</pre><p>Add a init body for the class</p><pre>package com.example.transformation<br><br>class AffineTransformer constructor(width: Int, height: Int) {<br>    <br>    <strong>init {<br>        <br>    }</strong><br>}</pre><p>Now we need to do some transformation to map the cartesian coordinate to screen/Canvas coordinates. The canvas has a height and width as shown in the image below. The direction of the y axis is inverted.</p><p>You can see the cartesian (1,1) is mapped to canvas (1, 15)</p><p>&amp;</p><p>Cartesian (2,15) is mapped to canvas (2,1)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d8Zs0ATGWATw0EO193ASfQ.png" /></figure><p>Without further explanation, let me introduce a translation for (x, y) value of screen coordinate in terms of the cartesian coordinates (primed)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*TmAFDa-MXEvgkalLfa7kAw.png" /></figure><p>These two equations can be written in a matrix form as</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*Y8fHwUjV8IxGhvgVvX7I0g.png" /></figure><p>Since this equation is in the Matrix form of X′ =AX+B so, it is an affine transformation.</p><p>The affine transformation can also be written, ignoring the last</p><p>value, as.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*RBWbYoamFNhwG6xa-O60_w.png" /></figure><p>This is equivalent to <strong>X′ =M.X</strong> where <strong>M</strong> is the android matrix</p><p>This is how Android Matrix works. In fact, we can work with this matrix with all the transformations.</p><p>In the future chapters, we will discuss in detail how these transformations are working and how the order of the transformation makes a difference.</p><h4>Using this transformation in Android Graphics</h4><p>Now we will see how to build this transformation in our AffineTransformer class. We need to look at some key points in our earlier equation. The x value doesn’t change.</p><p>And the Y value has two changes. One is the sign (direction) is changed. This can be considered as scaling in the negative direction. A shift in the positive direction of the screen happens. This shift is a translation of a point in the positive direction of the screen.</p><p>So the <strong>transformation</strong> is <strong>scaling</strong> and <strong><em>translation</em></strong></p><p>To start this let&#39;s define a Matrix</p><p>Let us define a Matrix instance in the AffineTransformer class as below.</p><pre>package com.example.transformation<br><br>import android.graphics.Matrix<br><br>class AffineTransformer constructor(width: Int, height: Int) {<br><br>    <strong>private var cartesianToAndroid   : Matrix = Matrix()</strong><br>    init {<br><br>    }<br>}</pre><p>At this point, we will set up the matrix for the cartesian to Android transformation.</p><pre>package com.example.transformation<br><br>import android.graphics.Matrix<br><br>class AffineTransformer constructor(width: Int, height: Int) {<br><br>    private var cartesianToAndroid   : Matrix = Matrix()<br>    init {<br>        <strong><em>//Tranformation for changing the orientation<br>        </em>cartesianToAndroid.setScale( 1f, -1f);<br>        <em>//Trnasformation for translation<br>        </em>cartesianToAndroid.postTranslate(0f, height.toFloat())</strong><br>    }<br>}</pre><p>Add a function to transform the input x and y value to the transformed point.</p><pre>package com.example.transformation<br><br>import android.graphics.Matrix<br>import android.graphics.PointF<br><br>class AffineTransformer constructor(width: Int, height: Int) {<br><br>    private var cartesianToAndroid   : Matrix = Matrix()<br>    init {<br>        <em>//Tranformation for changing the orientation<br>        </em>cartesianToAndroid.setScale( 1f, -1f);<br>        <em>//Trnasformation for translation<br>        </em>cartesianToAndroid.postTranslate(0f, <strong>height.toFloat()</strong>)<br>    }<br><br><strong>    fun tranFormPointToCartesian(x: Float, y: Float) : PointF {<br><br>        var dst = <em>floatArrayOf</em>(x, y)<br>        cartesianToAndroid.mapPoints(dst)<br>        return  PointF(dst[0], dst[1])<br>    }</strong><br>}</pre><p>In your TransformationView class add the code to initialize the AffineTransformer with width and height.</p><pre>package com.example.transformation<br><br>import android.content.Context<br>import android.graphics.Canvas<br>import android.graphics.Paint<br>import android.util.AttributeSet<br>import android.view.View<br><br>class TransformationView<br>@JvmOverloads<br>constructor(context: Context, attr: AttributeSet? = null, defStyleAttr:  Int = 0): View(context, attr, defStyleAttr) {<br>    private var drawPaint: Paint = Paint()<br>    private lateinit var transFormer: AffineTransformer<br>    init {<br>        drawPaint.<em>textSize </em>= 25f<br>    }<br><br>   <strong> @Override<br>    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int)<br>     {<br>        super.onSizeChanged(w, h, oldw, oldh)<br>        transFormer = AffineTransformer(w, h)<br>    }</strong><br><br>    override fun onDraw(canvas: Canvas?) {<br>        super.onDraw(canvas)<br>        canvas?.drawText(&quot;@(200, 200)&quot;, 200f, 200f, drawPaint)<br>        canvas?.drawText(&quot;@(400, 200)&quot;, 400f, 200f, drawPaint)<br>        canvas?.drawText(&quot;@(200, 400)&quot;, 200f, 400f, drawPaint)<br>        canvas?.drawText(&quot;@(400, 400)&quot;, 400f, 400f, drawPaint)<br>    }<br>}</pre><p>Introduce a new function <strong>drawText</strong> with will use the point transformation before drawing.</p><pre>package com.example.transformation<br><br>import android.content.Context<br>import android.graphics.Canvas<br>import android.graphics.Paint<br>import android.graphics.PointF<br>import android.util.AttributeSet<br>import android.view.View<br><br>class TransformationView<br>@JvmOverloads<br>constructor(context: Context, attr: AttributeSet? = null, defStyleAttr:  Int = 0): View(context, attr, defStyleAttr) {<br>    private var drawPaint: Paint = Paint()<br>    private lateinit var transFormer: AffineTransformer<br>    init {<br>        drawPaint.<em>textSize </em>= 25f<br>    }<br><br>    @Override<br>    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {<br>        super.onSizeChanged(w, h, oldw, oldh)<br>        transFormer = AffineTransformer(w, h)<br>    }<br><br><strong>    fun drawText(canvas: Canvas?, point: PointF, text: String) {<br>        var transFormedpoint = transFormer<br>              .tranFormPointToCartesian(point.x, point.y)<br>        canvas?.drawText(text, transFormedpoint.x, <br>               transFormedpoint.y, drawPaint)<br>    }</strong><br><br>    override fun onDraw(canvas: Canvas?) {<br>        super.onDraw(canvas)<br>        canvas?.drawText(&quot;@(200, 200)&quot;, 200f, 200f, drawPaint)<br>        canvas?.drawText(&quot;@(400, 200)&quot;, 400f, 200f, drawPaint)<br>        canvas?.drawText(&quot;@(200, 400)&quot;, 200f, 400f, drawPaint)<br>        canvas?.drawText(&quot;@(400, 400)&quot;, 400f, 400f, drawPaint)<br>    }<br>}</pre><p>Replace the old canvas.drawText function with this new function as below</p><pre>package com.example.transformation<br><br>import android.content.Context<br>import android.graphics.Canvas<br>import android.graphics.Paint<br>import android.graphics.PointF<br>import android.util.AttributeSet<br>import android.view.View<br><br>class TransformationView<br>@JvmOverloads<br>constructor(context: Context, attr: AttributeSet? = null, defStyleAttr:  Int = 0): View(context, attr, defStyleAttr) {<br>    private var drawPaint: Paint = Paint()<br>    private lateinit var transFormer: AffineTransformer<br>    init {<br>        drawPaint.<em>textSize </em>= 25f<br>    }<br><br>    @Override<br>    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {<br>        super.onSizeChanged(w, h, oldw, oldh)<br>        transFormer = AffineTransformer(w, h)<br>    }<br><br>    fun drawText(canvas: Canvas?, point: PointF, text: String) {<br>        var transFormedpoint = transFormer.tranFormPointToCartesian(point.x, point.y)<br>        canvas?.drawText(text, transFormedpoint.x, transFormedpoint.y, drawPaint)<br>    }<br><br>    override fun onDraw(canvas: Canvas?) {<br>        super.onDraw(canvas)<br><strong>        <em>// canvas?.drawText(&quot;(200, 200)&quot;, 200f, 200f, drawPaint)<br>        </em>drawText(canvas, PointF(200f, 200f), &quot;(200, 200)&quot;)<br>        <em>//canvas?.drawText(&quot;(400, 200)&quot;, 400f, 200f, drawPaint)<br>        </em>drawText(canvas, PointF(400f, 200f), &quot;(400, 200)&quot;)<br>        <em>//canvas?.drawText(&quot;(200, 400)&quot;, 200f, 400f, drawPaint)<br>        </em>drawText(canvas, PointF(200f, 400f), &quot;(200, 400)&quot;)<br>        <em>//canvas?.drawText(&quot;(400, 400)&quot;, 400f, 400f, drawPaint)<br>        </em>drawText(canvas, PointF(400f, 400f), &quot;(400, 400)&quot;)</strong><br>    }<br>}</pre><p>Run the code. And you will see that now the direction of the Y-axis is as in a cartesian coordinate system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ydo22uClnsEfjwhtYO2K5g.png" /></figure><p>Later we will discuss in more detail the <strong>Android Transformations</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c6c655752c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Graphics]]></title>
            <link>https://banchanattu.medium.com/android-graphics-a65e33ef86a2?source=rss-d60a7c2f155c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a65e33ef86a2</guid>
            <category><![CDATA[canvas-programing]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Banchanattu]]></dc:creator>
            <pubDate>Sat, 04 Sep 2021 22:39:12 GMT</pubDate>
            <atom:updated>2021-09-10T14:51:25.376Z</atom:updated>
            <content:encoded><![CDATA[<h4>Creating the first program</h4><p>Use the File-&gt;New-&gt;New Project to create a new project. Select the Empty Activity. Give a name for the</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*6obQ3U5aoFdaZloAjy511g.png" /></figure><p>This would create a project with a project with a Main activity that looks. The line <strong><em>setContentView</em></strong> shows which layout resource file is used in this activity.</p><pre>package com.example.tranformation<br><br>import androidx.appcompat.app.AppCompatActivity<br>import android.os.Bundle<br><br>class MainActivity : AppCompatActivity() {<br>    override fun onCreate(savedInstanceState: Bundle?) {<br>        super.onCreate(savedInstanceState)<br>        <strong><em>setContentView(R.layout.activity_main)</em></strong><br>    }<br>}</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28b519761fe53ea866c02e69087994c4/href">https://medium.com/media/28b519761fe53ea866c02e69087994c4/href</a></iframe><p>The content of the activity_main.xml looks like below</p><pre><em>&lt;?</em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<em>?&gt;<br></em>&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;.MainActivity&quot;&gt;<br><br>    &lt;TextView<br>        android:layout_width=&quot;wrap_content&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:text=&quot;Hello World!&quot;<br>        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;<br>        app:layout_constraintLeft_toLeftOf=&quot;parent&quot;<br>        app:layout_constraintRight_toRightOf=&quot;parent&quot;<br>        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;<br><br>&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;</pre><p>There is no great deal in this program; it is just a hello world program. You can run this on the device.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_8_HHfOsKkPkSvT0-xm74Q.png" /></figure><p>At this point, we will create a custom view which we will use to write our canvas graphics.</p><h4>Create a custom view for canvas drawing</h4><p>The next step we are trying is to create a custom view we will name this as my custom view. Let us name this as<strong><em> TranformationView. </em></strong>The reason for this name will be apparent later. This is subclassed from the View class. Also, you need to override the <strong><em>onDraw</em></strong> function.</p><pre>package com.example.transformation<br><br>import android.content.Context<br>import android.graphics.Canvas<br>import android.graphics.Paint<br>import android.util.AttributeSet<br>import android.view.View<br><br>class TransformationView<br>@JvmOverloads<br>constructor(context: Context, attr: AttributeSet? = null, defStyleAttr:  Int = 0): View(context, attr, defStyleAttr) {<br>    private var drawPaint: Paint = Paint()<br>    init {<br>        drawPaint.<em>textSize </em>= 25f<br>    }<br><br>    <strong>override fun onDraw(canvas: Canvas?) {<br>        super.onDraw(canvas)<br>    }</strong><br>}</pre><p>The <strong><em>onDraw </em></strong>function has an instance reference to canvas, which we will be using for our canvas drawing.</p><p>Let us add some functionality to draw some text at four different points on the canvas. Note the highlighted lines of code</p><p>The first parameter is the string to write. The second and third are the (X, Y) coordinates in terms of pixel to where the text should appear. The third parameter is the Paint to draw with.</p><pre>package com.example.transformation<br><br>import android.content.Context<br>import android.graphics.Canvas<br>import android.graphics.Paint<br>import android.util.AttributeSet<br>import android.view.View<br><br>class TransformationView<br>@JvmOverloads<br>constructor(context: Context, attr: AttributeSet? = null, defStyleAttr:  Int = 0): View(context, attr, defStyleAttr) {<br>    private var drawPaint: Paint = Paint()<br>    init {<br>        drawPaint.<em>textSize </em>= 25f<br>    }<br><br>    override fun onDraw(canvas: Canvas?) {<br>        super.onDraw(canvas)<br>       <strong> canvas?.drawText(&quot;@(200, 200)&quot;, 200f, 200f, drawPaint)<br>        canvas?.drawText(&quot;@(400, 200)&quot;, 400f, 200f, drawPaint)<br>        canvas?.drawText(&quot;@(200, 400)&quot;, 200f, 400f, drawPaint)<br>        canvas?.drawText(&quot;@(400, 400)&quot;, 400f, 400f, drawPaint)</strong><br>    }<br>}</pre><p>One more modification to use this new view as part of the Activity. In this, we should delete the <strong><em>TextView</em></strong> and replace it with <strong><em>TransformationView. </em></strong>Now you should see our drawText functions in action.</p><pre><em>&lt;?</em>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<em>?&gt;<br></em>&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;<br>    android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;match_parent&quot;<br>    tools:context=&quot;.MainActivity&quot;&gt;<br>   <strong> &lt;com.example.transformation.TransformationView<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;/&gt;</strong><br><br>&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;</pre><p>Here is the output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3SqzdycMqMgjihkpD1fLZA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a65e33ef86a2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>