Chapter 10. Just a Face for Your Time - WatchFace and Services
The face, also known as the dial, is the part of the clock that displays the time with fixed numbers with moving hands. The appearance of a clock face can be designed with various artistic approaches and creativity. Designing a conventional watch face is a beautiful art; a watch face artist will know what it takes to carve and engineer a watch face for traditional wearable watches. In Android Wear, the process is very similar, except you, being the watch face maker, will not have any tools in your hands, but will instead need to know which service you need to extend and what piece of code will help you customize the look and feel of the watch face. The watch face will show the time and date. Here, in Android Wear, a watch face can be analog or it can be digital.
Android Wear watch faces are services that are packaged inside a wearable app. When users select one of the available watch faces, the wearable device shows the watch...
The CanvasWatchFaceService class and registering your watch face
Watch faces are services with and visual rendering ability; all watch faces will extend the CanvasWatchFaceService
class. The CanvasWatchFaceService
class extracts its functionalities from the WallpaperSevice
and WallpaperService.Engine
classes. The Engine
class, with its methods, helps the watch face with its lifecycle. If you have to make a watch face for an Android Wear, you should use the CanvasWatchfaceService
class instead of plain old vanilla WallpaperService
. A watch face service, like a wallpaper service, must implement only the onCreateEngine()
method. Watch face engines need to implement the method onTimeTick()
to refresh the time and refresh the view and onAmbientModeChanged(boolean)
to switch between different version of watch faces, such as the grey mode and colorful watch face. Watch face engines in a like manner implement onInterruptionFilterChanged(int)
to update the view dependent upon how much information...
The CanvasWatchFaceService.Engine class
The CanvasWatchFaceService.Engine
class extends the WatchFaceService.Engine
class. Here, actual implementation of a watch face that draws on a canvas can be accomplished. We ought to implement onCreateEngine()
to reestablish your concrete engine implementation. CanvasWatchFaceService.Engine
has one public constructor with a couple of procedures to us to implement the watch face. How about we examine a couple of methods that we will in the later bit of this chapter:
void invalidate ()
: Plans a call to onDraw(Canvas, Rect)
to draw the following frame. This must be approached on the main thread.
void onDestroy ()
: In this callback, we can the hardware and other resources that we would be using to complete the watch face.
void onDraw(Canvas canvas, Rect bounds)
: Draws the face, all the visual components, and clock revive rationale, and other clock arrangements are accomplished in this method.void onSurfaceChanged()
: This takes four params, void onSurfaceChanged...
Writing your own watch face
Android Studio is the primary tool that we use to write Wear apps for numerous reasons; since we have already configured our development environment for Wear 2.0 development, it shouldn't be a challenge. Let's fire up Android Studio and create a Wear project.
In Activity chooser, select Add No Activity
. Since a watch face is a service, we don't need activity:
We have read in the previous section that we will be extending the class to CanvasWatchFaceService
, where watch face is being drawn on a canvas, and another class is CanvasWatchFaceService.Engine
, where we will work on the actual implementation of the watch face and more methods that we have discussed. It will help us to achieve the necessary setup for the watch face.
Now, let's create a class file called PacktWatchFace
in the package.
The PacktWatchFace
class will extend to the CanvasWatchFaceService
class:
After creating the class file, extend it to the CanvasWatchFaceService
class; this is the service class...
In this chapter, we have explored the fundamental understanding of designing watch faces and we have built a digital watch face. We have understood how the CanvasWatchFaceService
class helps in building watch faces and we have also seen the following watch face-related concepts:
- The
CanvasWatchFaceService
class - The
canvasWatchFaceService.Engine
method - Registering watch face in a Wear module manifest
- Handling tap gesture
- Different form factors
- Adding bitmap images to the watch face
- Watch face elements
- Common issues
- Interactive watch faces
Making watch faces is an excellent artistic engineering, including what data we should express in the watch face and how time and date data is being displayed. The ComplicationsAPI
is new in Wear 2.0. Let's discuss that in the next chapter along with a few advanced concepts.