Canvas API in Android Jetpack Compose
Last Updated :
20 Dec, 2021
Jetpack Compose is a modern UI toolkit that is designed to simplify UI development in Android. It consists of a reactive programming model with conciseness and ease of Kotlin programming language. It is fully declarative so that you can describe your UI by calling some series of functions that will transform your data into a UI hierarchy. When the data changes or is updated then the framework automatically recalls these functions and updates the view for you. Sometimes an app needs to draw some custom graphics on the screen and have precise control over what's been drawn on the screen. In this article, we will learn the basics of Canvas API in jetpack compose and try to create a GeeksforGeeks logo using canvas API. Below is the image of how it will look finally,
Step by Step Implementation
Step 1: Create a New Project
To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.
Step 2: Working with the MainActivity.kt file
Navigate to the app > java > your app’s package name and open the MainActivity.kt file.
Kotlin
@Composable
fun Icon(modifier: Modifier = Modifier) {
Canvas(modifier = modifier.size(100.dp), onDraw = {
val canvasWidth = size.width
val canvasHeight = size.height
// we first draw the arc which
// will be the curve of the logo
drawArc(
color = Color.White,
// arc starts at 0 degree and ends
startAngle = 0f,
// set use center to false to draw the
// arc without centered line
// Tip: use center to true to draw the arc
// with centered line and see the difference
useCenter = false,
// set the end angle of the arc
sweepAngle = 300f,
// set the width of the arc and
// how the arc cap will be drawn
// cap = StrokeCap.Round will draw
// the arc with rounded end
style = Stroke(width = 40f, cap = StrokeCap.Square)
)
})
}
If you call this composable from setContent you will see something like this.
Kotlin
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// CanvasApiTheme is Auto generated theme,
// It will be appnameTheme in your case
CanvasAPITheme {
Icon()
}
}
}
}
Now we need to add a line in the center. We will use the function drawLine in the canvas scope. Add this code below the arc so that it will be on top of the arc
Note: Order in which shapes are written in canvas scope is the order in which they will be placed on each other.
Kotlin
// draw the center line of the logo
drawLine(
color = Color.White,
// set the start point of the
// line to the center of the canvas
start = Offset(x = 0f, y = canvasHeight / 2),
// set the end point of the
// line to the center of the canvas
end = Offset(x = canvasWidth, y = canvasHeight / 2),
// set the width of the line
strokeWidth = 40f
)
Now if you run the app you will see complete G like this.
Our one side of the logo is complete, we just need to place two Icon composable in a row such that one is the mirror image of each other. Â MainActivity will now look like this. Refer to the comments for better understanding.
Kotlin
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CanvasAPITheme {
// Create a box to and set contentAlignment
// to Center to center the Icon
Box(
modifier = Modifier
.fillMaxSize()
.background(Color(0xFF308D46)),
contentAlignment = Alignment.Center
) {
// Create a row
Row {
// Place one Icon in the row but
// rotate Y Axis by -180 degree
// it will look like mirror image
Icon(Modifier.graphicsLayer(rotationY = -180f))
// set some space between the icons
Spacer(modifier = Modifier.width(20.dp))
// Original Icon without rotation
Icon()
}
}
}
}
}
}
Now run the app to see the final result.
Output:
In this article, we used Arc and Line. Canvas also provides the option to draw other shapes like
- drawRect to draw a rectangle.
- drawImage to draw the bitmap.
- drawRoundRect to draw a rounded rectangle.
- drawCircle to draw a circle.
- drawOval for oval.
- drawPath for drawing path.
- drawPoints to draw points given in the argument.
Similar Reads
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys
11 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read