Canvas API is also one of the most used in Android. The name of the API itself tells us that the API is being used for drawing on the drawing board. With the help of this API, we can draw different types of shapes and create custom UI components that are not present in Android. In this article, we will take a look at Canvas API and also use this API in our app to make a simple design.
What is Canvas API?
Canvas API is a drawing framework that is provided in Android, with the help of which we can create custom shapes like rectangle, circle, and many more in our UI design. With the help of this API, we can draw any type of shape for our app. The drawing of the different shapes is done using Bitmap.
Understanding the Working of Canvas API
While using this API the screen of the user’s device is called Canvas on which we have to draw different types of shapes and designs. There are different methods that are used to draw different shapes on our Canvas. Below are the methods which are used for drawing shapes on Canvas. Methods Description This method is used to measure the size of the view and the children’s present in that view. This method is use to draw the different views in our Canvas. With this method we can draw different shapes on our Canvas. There are predefined methods for different shapes such as drawRect(), drawArc(), drawLine() and many more.
onMeasure() onDraw() onLayout() This method helps us to set the size of the view.
This method is used to measure the size of the view
and the children’s present in that view.
This method is use to draw the different views in our Canvas.
With this method we can draw different shapes on our Canvas.
There are predefined methods for different shapes such as drawRect(),
drawArc(), drawLine() and many more.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Step 3: Creating a new Java class for drawing our view
Navigate to the app > java > your app’s package name > Right-click on it > New > Java class and name it as PaintView and add the below code to it.
Step 4: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Now run your app and see the output of the app.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.