adroid Wear-Designing Watch Faces and Building a Watch Face Service

desaco發表於2016-01-16

  Android Wear enables you to create custom watch faces for Wear devices. When users install a handheld app that contains awearable app with watch faces, they become available in the Android Wear companion app on the handheld device and in the watch face picker on the wearable device.

 Note: We recommend using Android Studio for Android Wear development, as it provides project setup, library inclusion, and packaging conveniences. 

 > Designing Watch Faces

 Similar to the process of designing a traditional watch face, creating one for Android Wear is an exercise in visualizing time clearly. Android Wear devices provide advanced capabilities for watch faces that you can leverage in your designs, such as vibrant colors, dynamic backgrounds, animations, and data integration. However, there are also many design considerations that you must take into account.

> As you plan the look of your watch face and what kind of information it should present to users, consider these design guidelines:


Figure 1. Example watch faces.

Plan for square and round devices
Your design should work for both square and round Android Wear devices, including devices with insets on the bottom of the screen.
Support all display modes
Your watch face should support ambient mode with limited color and interactive mode with full color and animations.
Optimize for special screen technologies
In ambient mode, your watch face should keep most pixels black. Depending on the screen technology, you may need to avoid large blocks of white pixels, use only black and white, and disable anti-aliasing.
Accommodate system UI elements
Your design should ensure that system indicators remain visible and that users can still read the time when notification cards appear on the screen.
Integrate data
Your watch face can leverage sensors and cellular connectivity on the companion mobile device to show user data relevant to the context, such as the weather for the day or their next calendar event.
Provide configuration options
You can let users configure some aspects of your design (like colors and sizes) on the wearable or on the Android Wear companion app.
 To improve performance, you should scale the background image only once and store the resulting bitmap.

 To increase battery life, the application code that draws your watch face in ambient mode should be relatively simple. You usually draw outlines of shapes using a limited set of colors in this mode. In interactive mode, you can use full color, complex shapes, gradients, and animations to draw your watch face.

> Building a Watch Face Service

 Watch faces in Android Wear are implemented asservices and packaged inside a wearable app. When users install a handheld app that contains a wearable app with watch faces, these watch faces become available in the Android Wear companion app on the handheld device and in the watch face picker on the wearable. When users select one of the available watch faces, the wearable device shows the watch face and invokes its service callback methods as required.

 Android Studio creates a project with the wear and mobile modules. 

 The Wearable Support Library provides the necessary classes that you extend to create watch face implementations. The Google Play services client libraries (play-services and play-services-wearable) are required to sync data items between the companion device and the wearable with the Wearable Data Layer API.

 Android Studio automatically adds the required entries in your build.gradle files when you create the project in the instructions above.

 Note: We recommend using Android Studio for Android Wear development, as it provides project setup, library inclusion, and packaging conveniences.

>Watch faces require the PROVIDE_BACKGROUND and WAKE_LOCK permissions. 

<uses-permission
        android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
    <uses-permission
        android:name="android.permission.WAKE_LOCK" />
To implement a watch face, you extend the CanvasWatchFaceService and CanvasWatchFaceService.Engineclasses, and then you override the callback methods in the CanvasWatchFaceService.Engine class. These classes are included in the Wearable Support Library.

The following snippet outlines the key methods you need to implement:

public class AnalogWatchFaceService extends CanvasWatchFaceService {

    @Override
    public Engine onCreateEngine() {
        /* provide your watch face implementation */
        return new Engine();
    }

    /* implement service callback methods */
    private class Engine extends CanvasWatchFaceService.Engine {

        @Override
        public void onCreate(SurfaceHolder holder) {
            super.onCreate(holder);
            /* initialize your watch face */
        }

        @Override
        public void onPropertiesChanged(Bundle properties) {
            super.onPropertiesChanged(properties);
            /* get device features (burn-in, low-bit ambient) */
        }

        @Override
        public void onTimeTick() {
            super.onTimeTick();
            /* the time changed */
        }

        @Override
        public void onAmbientModeChanged(boolean inAmbientMode) {
            super.onAmbientModeChanged(inAmbientMode);
            /* the wearable switched between modes */
        }

        @Override
        public void onDraw(Canvas canvas, Rect bounds) {
            /* draw your watch face */
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            /* the watch face became visible or invisible */
        }
    }
}

  The CanvasWatchFaceService class provides an invalidate mechanism similar to the View.invalidate()method. You can call the invalidate() method throughout your implementation when you want the system to redraw the watch face. You can only use the invalidate() method in the main UI thread. To invalidate the canvas from another thread, call the postInvalidate() method.

>how to register a watch face implementation under the application element:

<service
    android:name=".AnalogWatchFaceService"
    android:label="@string/analog_name"
    android:allowEmbedded="true"
    android:taskAffinity=""
    android:permission="android.permission.BIND_WALLPAPER" >
    <meta-data
        android:name="android.service.wallpaper"
        android:resource="@xml/watch_face" />
    <meta-data
        android:name="com.google.android.wearable.watchface.preview"
        android:resource="@drawable/preview_analog" />
    <meta-data
        android:name="com.google.android.wearable.watchface.preview_circular"
        android:resource="@drawable/preview_analog_circular" />
    <intent-filter>
        <action android:name="android.service.wallpaper.WallpaperService" />
        <category
            android:name=
            "com.google.android.wearable.watchface.category.WATCH_FACE" />
    </intent-filter>
</service>

The android.service.wallpaper metadata entry specifies the watch_face.xml resource file, which contains a wallpaper element:

<?xml version="1.0" encoding="UTF-8"?>
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" />

Your wearable app can contain more than one watch face. You must add a service entry to the manifest file of the wearable app for each of your watch face implementations.

相關文章