In this article i am going to show you how to draw a simple smiley face in Android using Canvas and in IOS using UIBezierPath.
Android
After following this article you should be able to create something like
Step 1 - Planing the smiley
Draw a circle with maximum radius, it will act as face.
Draw two small circles inside the face circle, it will act as eyes
Draw an arc under the eyes, it will act as the mouth.
Make sure, it works even if the orientation changes (both landscape and portrait modes.)
Step 2 - Create Required files.
Create Main Activity (Generate both xml and java)
Create FaceView class and extend from View.
Step 3 - Initializing the FaceView.
Create a FaceView constructor and initialize Paint object.
Setup default values like color, stroke values, radius
Step 4 - Override onDraw Function.
Set color property to paint object
Set stroke width to the paint object
Set drawing style as stroke
Step 5 - Draw Face circle.
Get the total width, height taken by our FaceView using getMeasuredWidth and getMeasuredHeight function.
DrawCircle function requires (x, y) points , radius and a paint object.
Calculate the center x position by getMeasuredWidth / 2
Calculate the center y position by getMeasuredHeight / 2
Calculate the radius from the minimum of y position or x position to support both landscape and portrait modes.
Call canvas.drawCircle function with the x position, y position, radius and paint object.
Step 5 - Draw eyes.
Find the eyeYposition. eyeYPosition will remail same for both left and right. The eyeYposition should be a little more than the calculated yPosition of the face. The Face’s yPosition is half the height, but eyes should be placed a little above. so i use the following formula to calculate the eyeYPosition
Find the leftEyeXPosition. xPosition is half the width, so the left eye position should take half the xPosition. i use the following formula to calculate leftEyeXPosition to make sure it works even the orientation changes.
Find the rightEyeXPosition. xPosition is half the width, so the right eye position should take little more than the xPosition. i use the following formula to calculate rightEyeXPosition to make sure it works even the orientation changes.
We have calculated the eye positions, now we can draw left, right eyes using drawCircle function.
Step 5 - Draw Mouth.
Drawing Mouth is little tricky.
1 Mark the boundaries using RectF
Find the top left - (leftEyeXPosition, yPosition + some value)
Find the bottom right - (rightEyeXPosition, yPosition + some value)
2 Draw an arc within the boundaries.
For happy face start from 0 degree and draw another 180 degree (sweep angle).
For sad face start from 180 degree and draw another 180 degree (sweep angle).
The final code of FaceView.
Step 3
Add the FaceView in MainActivity’s xml file.
IOS - swift
Now lets see how to do the same in IOS, I am going to use swift. The final output will be something like this.
Step 1 - Planing the smiley
Create the required files (Stroyboard, ViewController, FaceView)
Set up the storyboard and view controller to draw FaceView
Draw a circle with maximum radius, it will act as face.
Draw two small circles inside the face circle, it will act as eyes
Draw an arc under the eyes, it will act as the mouth.
Make sure, it works even if the orientation changes (both landscape and portrait modes.)
Step 2 - Creating required files
Open storyboard - navigate to object library, drag a View into the storyboard view controller.
Stretch the view to fit on the view controller.
Select the view and click the Reset to suggested constrains from the resolve auto layout button.
Create FaceView as a subclass of CocoaTouch UIView class.
Select the view from storyboard by shift-ctrl-click on the storyboard.
Click identity inspector and change the class to FaceView
Step 3 - Draw the Face
Setting up computed property to get the face center.
Setting up computed property for face radius. Face should take as maximum space as possible.
Create a facePath inside drawRect function using faceCenter, faceRadius.
Setting up line width using property observer
Setting up color using property absorber.
Set the color inside your drawRect function
Draw the face. using facePath.stroke()
The drawRect function looks something like
Step 4 - Some more changes
1 Redraw when the bounds changes. - To avoid the stretching while changing the orientation.
Click the view, select the Attribute inspector, change the mode to Redraw.
2 Reduce the radius to 90% of the width / height
3 Multiply 0.90 with the computed radius value as follows.
Step 5 - Drawing eyes.
To create eye we need some constants. we put them inside a struct.
To draw eye.
Drawing left eye
Drawing right eye
Step 5 - Drawing Mouth.
Step 5 - Final FaceView class.
The final FaceView class looks something like this.