Open In App

Create Polygons with Rounded Corners in Android using Jetpack Compose

Improve
Improve
Like Article
Like
Save
Share
Report

In Android, we can easily draw objects using the Canvas function. In the case of a complex object, we can create a path and load this path in the canvas for creating the object. To add customized effects, such as rounded corners, we can add path effects inside the canvas.

Polygons with Rounded Corners

 

So in this article, we will show you how you could Create Polygons with Rounded Corners in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.roundedcornerpolygon
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold 
            // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Rounded Corner Polygon", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Creating a canvas and creating a triangular path
                        Canvas(modifier = Modifier.fillMaxWidth().aspectRatio(1f)) {
                            val rect = Rect(Offset.Zero, size)
                            val trianglePath = Path().apply {
                                moveTo(rect.topCenter.x, rect.topCenter.y)
                                lineTo(rect.bottomRight.x, rect.bottomRight.y)
                                lineTo(rect.bottomLeft.x, rect.bottomLeft.y)
                                close()
                            }
  
                            // Adding a path effect of rounded corners
                            drawIntoCanvas { canvas ->
                                canvas.drawOutline(
                                    outline = Outline.Generic(trianglePath),
                                    paint = Paint().apply {
                                        color = Color.Green
                                        pathEffect = PathEffect.cornerPathEffect(rect.maxDimension / 3)
                                    }
                                )
                            }
                        }
  
                    }
                }
            )
        }
    }
}


Output:

You can see that the triangle created has rounded corners.

Output

 



Last Updated : 29 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads