android wear-Addressing Common Issues,Optimizing Performance and Battery Life

desaco發表於2016-01-16

> Addressing Common Issues

 Creating a custom watch face for Android Wear is substantially different from creating notifications and wearable-specific activities.

 Android Wear lets your watch face determine the screen shape at runtime. To detect whether the screen is square or round, override the onApplyWindowInsets() method in the CanvasWatchFaceService.Engine class as follows:

private class Engine extends CanvasWatchFaceService.Engine {
    boolean mIsRound;
    int mChinSize;

    @Override
    public void onApplyWindowInsets(WindowInsets insets) {
        super.onApplyWindowInsets(insets);
        mIsRound = insets.isRound();
        mChinSize = insets.getSystemWindowInsetBottom();
    }
    ...
}

 To ensure that the system indicators remain visible, you can configure their position on the screen and whether they need background protection when you create a WatchFaceStyle instance:

  • To set the position of the status bar, use the setStatusBarGravity()method.
  • To set the position of the hotword, use thesetHotwordIndicatorGravity() method.
  • To protect the status bar and hotword with a semi-transparent gray background, use the setViewProtection() method. This is usually necessary if your watch face has a light background, since the system indicators are white.
 When you draw your watch face, obtain the size of the canvas with the Canvas.getWidth() andCanvas.getHeight() methods and set the positions of your graphic elements using values that are some fraction of the detected screen size. If you resize the elements of your watch face in response to a peek card, use values that are some fraction of the remaining space above the card to redraw your watch face.

In addition to accommodating notification cards and system indicators, you need to ensure that the animations in your watch face run smoothly and that your service does not perform unnecessary computations. 

>Optimizing Performance and Battery Life

 In addition to accommodating notification cards and system indicators, you need to ensure that the animations in your watch face run smoothly and that your service does not perform unnecessary computations. Watch faces in Android Wear run continuously on the device, so it is critical that your watch face uses power efficiently.

 >To improve the performance of your watch face:

  • Do not use graphic elements that are larger than you need.
  • Remove extra transparent pixels around the edges.
 When you draw a scaled bitmap on the Canvas object using the Canvas.drawBitmap() method, you can provide a Paint instance to configure several options. To improve performance, disable anti-aliasing using thesetAntiAlias() method, since this option does not have any effect on bitmaps.

  》When possible, avoid performing these operations inside the Engine.onDraw() method:

  • Loading images and other resources.
  • Resizing images.
  • Allocating objects.
  • Performing computations whose result does not change between frames.
 Animations are often computationally expensive and consume a significant amount of power. Most animations look fluid at 30 frames per second, so you should avoid running your animations at a higher frame rate.

 Animations and small changes to the contents of the watch face wake up the CPU. Your watch face should let the CPU sleep in between animations. To maximize battery life, use animations sparingly. Even a blinking colon wakes up the CPU with every blink and hurts battery life.

 The Android Wear companion app lets developers and users see how much battery different processes on the wearable device are consuming under Settings > Watch battery.

For more information about new features in Android 5.0 that help you improve battery life, see Project Volta.

相關文章