1page.title=Optimizing Performance and Battery Life
2
3@jd:body
4
5<div id="tb-wrapper">
6<div id="tb">
7<h2>This lesson teaches you to</h2>
8<ol>
9  <li><a href="#ReduceSize">Reduce the Size of Your Bitmap Assets</a></li>
10  <li><a href="#CombineBitmaps">Combine Bitmap Assets</a></li>
11  <li><a href="#AntiAlias">Disable Anti-Aliasing when Drawing Scaled Bitmaps</a></li>
12  <li><a href="#OutDrawing">Move Expensive Operations Outside the Drawing Method</a></li>
13  <li><a href="#SavePower">Follow Best Practices to Save Power</a></li>
14</ol>
15<h2>You should also read</h2>
16<ul>
17  <li><a href="{@docRoot}design/wear/watchfaces.html">Watch Faces for Android Wear</a></li>
18</ul>
19</div>
20</div>
21
22<p>In addition to accommodating notification cards and system indicators, you need to ensure that
23the animations in your watch face run smoothly and that your service does not perform unnecessary
24computations. Watch faces in Android Wear run continuously on the device, so it is critical
25that your watch face uses power efficiently.</p>
26
27<p>This lesson provides some tips to speed up your animations and to measure and conserve
28power on the device.</p>
29
30
31
32<h2 id="ReduceSize">Reduce the Size of Your Bitmap Assets</h2>
33
34<p>Many watch faces consist of a background image and other graphic assets that are transformed
35and overlapped on top of the background image, such as clock hands and other elements of the design
36that move over time. Typically these graphic elements are rotated (and sometimes scaled) inside the
37<a href="{@docRoot}reference/android/support/wearable/watchface/CanvasWatchFaceService.Engine.html#onDraw(android.graphics.Canvas, android.graphics.Rect)"><code>Engine.onDraw()</code></a>
38method every time the system redraws the watch face, as described in
39<a href="{@docRoot}training/wearables/watch-faces/drawing.html#Drawing">Draw Your Watch
40Face</a>.</p>
41
42<p>The larger these graphic assets are, the more computationally expensive it is to transform them.
43Transforming large graphic assets in the
44<a href="{@docRoot}reference/android/support/wearable/watchface/CanvasWatchFaceService.Engine.html#onDraw(android.graphics.Canvas, android.graphics.Rect)"><code>Engine.onDraw()</code></a>
45method drastically reduces the frame rate at which the system can run your animations.</p>
46
47<div id="fig1" style="float:right;width:250px;margin-left:25px">
48<img src="{@docRoot}training/wearables/watch-faces/images/ClockHandFull.png" alt=""
49     width="180" height="180"/>
50<img src="{@docRoot}training/wearables/watch-faces/images/ClockHandCropped.png" alt=""
51     width="15" height="65" style="margin-left:25px"/>
52<p class="img-caption">
53<strong>Figure 1.</strong> Clock hands can be cropped to remove extra pixels.</p>
54</div>
55
56<p>To improve the performance of your watch face:</p>
57
58<ul>
59<li>Do not use graphic elements that are larger than you need.</li>
60<li>Remove extra transparent pixels around the edges.</li>
61</ul>
62
63<p>The example clock hand on the left side of <a href="#fig1">Figure 1</a> can be reduced in size
64by 97&#37;.</p>
65
66<p>Reducing the size of your bitmap assets as described in this section not only improves
67the performance of your animations, but it also saves power.</p>
68
69
70
71<h2 id="CombineBitmaps">Combine Bitmap Assets</h2>
72
73<p>If you have bitmaps that are often drawn together, consider combining them into the same
74graphic asset. You can often combine the background image in interactive mode with the tick
75marks to avoid drawing two full-screen bitmaps every time the system redraws the watch face.</p>
76
77
78
79<h2 id="AntiAlias">Disable Anti-Aliasing when Drawing Scaled Bitmaps</h2>
80
81<p>When you draw a scaled bitmap on the {@link android.graphics.Canvas} object using the {@link
82android.graphics.Canvas#drawBitmap(android.graphics.Bitmap, float, float, android.graphics.Paint)
83Canvas.drawBitmap()} method, you can provide a {@link android.graphics.Paint} instance to configure
84several options. To improve performance, disable anti-aliasing using the {@link
85android.graphics.Paint#setAntiAlias setAntiAlias()} method, since this option does not have any
86effect on bitmaps.</p>
87
88<div id="fig2" style="float:right;width:250px;margin-left:40px;margin-top:12px">
89<img src="{@docRoot}training/wearables/watch-faces/images/BitmapFilterDisabled.png" alt=""
90     width="70" height="70" style="margin-left:20px"/>
91<img src="{@docRoot}training/wearables/watch-faces/images/BitmapFilterEnabled.png" alt=""
92     width="70" height="70" style="margin-left:20px"/>
93<p class="img-caption"><strong>Figure 2.</strong> Example of bitmap filtering disabled (left) and
94enabled (right).</p>
95</div>
96
97<h3 id="BitmapFiltering">Use bitmap filtering</h3>
98
99<p>For bitmap assets that you draw on top of other elements, enable bitmap filtering on the same
100{@link android.graphics.Paint} instance using the {@link android.graphics.Paint#setFilterBitmap
101setFilterBitmap()} method. <a href="#fig2">Figure 2</a> shows a magnified view of a clock hand with
102and without bitmap filtering.</p>
103
104<p class="note"><strong>Note:</strong> In low-bit ambient mode, the system does not reliably
105render the colors in the image for bitmap filtering to process successfully. When ambient mode is
106active, disable bitmap filtering.</p>
107
108<h2 id="OutDrawing">Move Expensive Operations Outside the Drawing Method</h2>
109
110<p>The system calls the
111<a href="{@docRoot}reference/android/support/wearable/watchface/CanvasWatchFaceService.Engine.html#onDraw(android.graphics.Canvas, android.graphics.Rect)"><code>Engine.onDraw()</code></a>
112method every time it redraws your watch face, so you should only include operations that are
113strictly required to update the watch face inside this method to improve performance.<p>
114
115<p>When possible, avoid performing these operations inside the
116<a href="{@docRoot}reference/android/support/wearable/watchface/CanvasWatchFaceService.Engine.html#onDraw(android.graphics.Canvas, android.graphics.Rect)"><code>Engine.onDraw()</code></a>
117method:</p>
118
119<ul>
120<li>Loading images and other resources.</li>
121<li>Resizing images.</li>
122<li>Allocating objects.</li>
123<li>Performing computations whose result does not change between frames.</li>
124</ul>
125
126<p>You can usually perform these operations in the
127<a href="{@docRoot}reference/android/support/wearable/watchface/WatchFaceService.Engine.html#onCreate(android.view.SurfaceHolder)"><code>Engine.onCreate()</code></a>
128method instead.
129You can resize images ahead of time in the {@link
130android.service.wallpaper.WallpaperService.Engine#onSurfaceChanged(android.view.SurfaceHolder, int, int, int)
131Engine.onSurfaceChanged()} method, which provides you with the size of the canvas.</p>
132
133<p>To analyze the performance of your watch face, use the Android Device Monitor. In particular,
134ensure that the execution time for your
135<a href="{@docRoot}reference/android/support/wearable/watchface/CanvasWatchFaceService.Engine.html#onDraw(android.graphics.Canvas, android.graphics.Rect)"><code>Engine.onDraw()</code></a>
136implementation is short and
137consistent across invocations. For more information, see
138<a href="{@docRoot}tools/debugging/ddms.html">Using DDMS</a>.</p>
139
140
141
142<h2 id="SavePower">Follow Best Practices to Save Power</h2>
143
144<p>In addition to the techniques described in the previous sections, follow the best
145practices in this section to reduce the power consumption of your watch face.</p>
146
147<h3>Reduce the frame rate of animations</h3>
148
149<p>Animations are often computationally expensive and consume a significant amount of power. Most
150animations look fluid at 30 frames per second, so you should avoid running your animations
151at a higher frame rate.</p>
152
153<h3>Let the CPU sleep</h3>
154
155<p>Animations and small changes to the contents of the watch face wake up the CPU. Your watch
156face should let the CPU sleep in between animations. For example, you can use short bursts of
157animation every second in interactive mode and then let the CPU sleep until the next second.
158Letting the CPU sleep often, even briefly, can significantly reduce power consumption.</p>
159
160<p>To maximize battery life, use animations sparingly. Even a blinking colon wakes up the CPU with
161every blink and hurts battery life.</p>
162
163<h3>Monitor power consumption</h3>
164
165<p>The <a href="https://play.google.com/store/apps/details?id=com.google.android.wearable.app&hl=en">
166Android Wear companion app</a> lets developers and users see how much battery different processes
167on the wearable device are consuming under <strong>Settings</strong> > <strong>Watch
168battery</strong>.</p>
169
170<p>For more information about new features in Android 5.0 that help you improve battery life,
171see <a href="{@docRoot}about/versions/android-5.0.html#Power">Project Volta</a>.</p>
172