Optimizing Performance and Battery Life | Android Developers
Show navigation Hide navigation

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.

This lesson provides some tips to speed up your animations and to measure and conserve power on the device.

Reduce the Size of Your Bitmap Assets

Many watch faces consist of a background image and other graphic assets that are transformed and overlapped on top of the background image, such as clock hands and other elements of the design that move over time. Typically these graphic elements are rotated (and sometimes scaled) inside the Engine.onDraw() method every time the system redraws the watch face, as described in Draw Your Watch Face.

The larger these graphic assets are, the more computationally expensive it is to transform them. Transforming large graphic assets in the Engine.onDraw() method drastically reduces the frame rate at which the system can run your animations.

Figure 1. Clock hands can be cropped to remove extra pixels.

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.

The example clock hand on the left side of Figure 1 can be reduced in size by 97%.

Reducing the size of your bitmap assets as described in this section not only improves the performance of your animations, but it also saves power.

Combine Bitmap Assets

If you have bitmaps that are often drawn together, consider combining them into the same graphic asset. You can often combine the background image in interactive mode with the tick marks to avoid drawing two full-screen bitmaps every time the system redraws the watch face.

Disable Anti-Aliasing when Drawing Scaled Bitmaps

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 the setAntiAlias() method, since this option does not have any effect on bitmaps.

Figure 2. Example of bitmap filtering disabled (left) and enabled (right).

Use bitmap filtering

For bitmap assets that you draw on top of other elements, enable bitmap filtering on the same Paint instance using the setFilterBitmap() method. Figure 2 shows a magnified view of a clock hand with and without bitmap filtering.

Note: In low-bit ambient mode, the system does not reliably render the colors in the image for bitmap filtering to process successfully. When ambient mode is active, disable bitmap filtering.

Move Expensive Operations Outside the Drawing Method

The system calls the Engine.onDraw() method every time it redraws your watch face, so you should only include operations that are strictly required to update the watch face inside this method to improve performance.

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.

You can usually perform these operations in the Engine.onCreate() method instead. You can resize images ahead of time in the Engine.onSurfaceChanged() method, which provides you with the size of the canvas.

To analyze the performance of your watch face, use the Android Device Monitor. In particular, ensure that the execution time for your Engine.onDraw() implementation is short and consistent across invocations. For more information, see Using DDMS.

Follow Best Practices to Save Power

In addition to the techniques described in the previous sections, follow the best practices in this section to reduce the power consumption of your watch face.

Reduce the frame rate of animations

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.

Let the CPU sleep

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. For example, you can use short bursts of animation every second in interactive mode and then let the CPU sleep until the next second. Letting the CPU sleep often, even briefly, can significantly reduce power consumption.

To maximize battery life, use animations sparingly. Even a blinking colon wakes up the CPU with every blink and hurts battery life.

Monitor power consumption

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.