Issue:

Paint the white background and draw 10 circles growing from the middle of the screen.

1 – We created a project called: Projecto037

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

 

Lesson 33 - Draw: circles in kotlin - Lesson 33 - Draw: circles in kotlin - Lesson 33 - Draw: circles in kotlin -

Now we encode the class in Kotlin where all logic is found:

package com.coding180.project027



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

class MainActivity: AppCompatActivity () {
// coding180.com
    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)
    }

    class Canvass (context: Context): View (context) {

        override fun onDraw (canvas: Canvas) {
            canvas.drawRGB (255, 255, 255)
            val width = getWidth ()
            val hieght = getHeight ()
            val brush1 = Paint ()
            brush1.setARGB (255, 255, 0, 0)
            brush1.setStyle (Paint.Style.STROKE)
            for (f in 0..9)
                canvas.drawCircle ((width / 2) .toFloat (), (hieght / 2) .toFloat (), (f * 15) .toFloat (), brush1)
        }
    }
}

We paint the white background and we obtain the width and height of the control:

        Override fun onDraw (canvas: Canvas) {
            Canvas.drawRGB (255, 255, 255)
            Val width = getWidth ()
            High val = getHeight ()

We create an object of the class Paint, we fix the color red and through setStyle we indicate that only the perimeter must be painted:

            Val brush1 = Paint ()
            brush1.setARGB (255, 255, 0, 0)
            brush1.setStyle (Paint.Style.STROKE)

We have a for, for drawing the 10 concentric circles (we indicate in the first two parameters the center point of the circle and in the third parameter the radius of the circle:

            For (f in 0..9) 
                Canvas.drawCircle ((width / 2) .toFloat (), (high / 2) .toFloat (), (f * 15) .toFloat (), brush1)

The preview of the application is:

 

Lesson 33 - Draw: circles in kotlin - Lesson 33 - Draw: circles in kotlin - Lesson 33 - Draw: circles 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!