Issue:

Paint the white background and draw a series of rectangles with different styles.

1 – We created a project called: Proyecto036

We delete the TextView that automatically adds the Android Studio and define the ConstraintLayout id with the value: layout1:

 

Lesson 32 - Draw: rectangles in Kotlin - Lesson 32 - Draw: rectangles in Kotlin - Lesson 32 - Draw: rectangles in Kotlin -

Now we encode the class where the whole logic is:

package com.coding180.project036

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.opengl.ETC1.getWidth
import android.view.View


class MainActivity: AppCompatActivity () {

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)

        val layout1 = findViewById (R.id.layout1) as android.support.constraint.ConstraintLayout
        val background = Canvass (this)
        layout1.addView (background)
    }
// coding180.com
    class Canvass (context: Context): View (context) {

        override fun onDraw (canvas: Canvas) {
            canvas.drawRGB (255, 255, 255)
            val width = getWidth ()
            val brush1 = Paint ()

            brush1.setARGB (255, 255, 0, 0)
            canvas.drawRect (10f, 10f, (width - 10) .toFloat (), 40f, brush1)

            brush1.setStyle (Paint.Style.STROKE)
            canvas.drawRect (10f, 60f, (width - 10) .toFloat (), 90f, brush1)

            brush1.setStrokeWidth (3f)
            canvas.drawRect (10f, 110f, (width - 10) .toFloat (), 140f, brush1)

        }
    }
}

 

 

In the onDraw method of the Canvas class we proceed to paint the white background:

            canvas.drawRGB (255, 255, 255)

We get the width of the device:

            val width = getWidth ()

We create an object of class Paint:

            val brush1= Paint ()

We activate the color red:

            brush1.setARGB (255, 255, 0, 0)

We draw a rectangle from the coordinate column: 10 and row 10 to the column that matches the width of the screen minus 10 pixels and row 40. In addition, we pass the brush to use:

            canvas.drawRect (10f, 10f, (width - 10) .toFloat (), 40f, brush1)

For the next rectangle we set the brush to just paint the perimeter by calling the setStyle method and passing the constant STROKE:

            brush1.setStyle (Paint.Style.STROKE)
            canvas.drawRect (10f, 60f, (width - 10) .toFloat (), 90f, brush1)

Finally, we draw another rectangle that only the perimeter is painted but we change the thickness of the pencil by calling the setStrokeWidth method:

            brush1.setStrokeWidth (3f)
            canvas.drawRect (10f, 110f, (width - 10) .toFloat (), 140f, brush1)

The preview of the application is:

Lesson 32 - Draw: rectangles in Kotlin - Lesson 32 - Draw: rectangles in Kotlin - Lesson 32 - Draw: rectangles in Kotlin -

……………

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!