Android Developers Blog

Source: Internet
Author: User
<span id="Label3"></p>Making a performant watch face<p><p><em>Posted by Hoi Lam, Developer advocate, Android Wear</em></p></p><p><p>What ' s a better holiday gift than great performance? You ' ve got a great watch face idea – now, you want to make sure the face you ' re presenting to the world are one of the care an D attention to Detail.</p></p><p><p>At the core of the "watch face" is a process is a OnDraw method for canvas Operations. This allows maximum flexibility for your design, but also comes with a few performance Caveats. In this blog post, we'll mainly focus on performance using the real life journey of how we optimised the Santa Tracker W Atch face, more than doubling the number of FPS (from + fps to Fps) and making the Animation-sub-pixel Smooth.</p></p>Starting point-18 fps<p><p>Our Santa watch face contains a number of overlapping bitmaps that is used to achieve our final image. Here's a list of them from bottom to top:</p></p> <ol type="1"> <ol type="1"> <li>Background (static)</li> <li>Clouds which move to the middle</li> <li>Tick Marks (static)</li> <li>Santa figure and Sledge (static)</li> <li>Santa ' s hands-hours and minutes</li> <li>Santa ' s head (static)</li> </ol> </ol><p><p>The journey begins with these images ...</p></p>Large images Kill performance (+14 Fps)<p><p>Image size is critical-performance in a Wear application, especially if the images would be scaled and rotated. Wasted pixel space (like Santa's arm here) is a common asset mistake:</p></p> <table border="0"> <tbody> <tr> <td>before:584 x 584 = 341,056 pixels</td> <td>after:48*226 = 10,848 (97% Reduction)</td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table><p><p> It ' s tempting to use bitmaps from the original mock up that has the exact location of watch arms and components in ABS Olute Space. sadly, this creates problems, as in Santa's arm here. While the arm was in the correct position, even transparent pixels increase the size of the image, which can cause Performa NCE problems due to memory Fetch. You'll want to work with your design team to extract padding and rotational information from the images, and rely on the S Ystem to apply the transformations in our behalf. </p></p><p><p><span style="color: #ff0000;">Since The original image covers the entire screen, even though the bitmap is mostly transparent, the system still need s to check every pixel to see if they has been impacted.</span> Cutting down the area results in significant gains in Performance. After correcting both of the arms, the Santa watch is frame rate increased by ten fps to + fps (fps up 56%). We saved another 4 fps (fps up 22%) by cropping Santa's face and figure Layer. + fps gained, not bad!</p></p>Combine Bitmaps (+7 Fps)<p><p>Although it would is ideal to has the watch tick marks on top of our clouds, it actually does don't make much difference VI Sually as the clouds themselves is Transparent. Therefore there is a opportunity to combine, the background with the ticks.</p></p>+<p><p>When we combined these-together, it meant, the watch needed to spend less time doing alpha blending operatio NS between them, saving precious CPU Time. so, consider collapsing alpha blended resources wherever we can in order to increase performance. By combining-bitmaps, we were able to gain another 7 fps (fps up 39%).</p></p>Anti-alias vs Filterbitmap Flags-what should you use? (+2 Fps)<p><p>Android Wear watches come in all shapes and sizes. As a result, it is sometimes necessary to resize a bitmap before drawing on the Screen. however, It isn't always clear about the options developers should select to make sure that the bitmap comes out smoothly. With canvas.drawbitmap, developers need to feeds in a Paint object. There is important options to Set-they is Anti-alias and filterbitmap. Here's our Advice:</p></p> <ul> <ul> <ul> <li> <span style="color: #ff0000;"> <strong> Anti-alias does not does anything for bitmaps with transparent Edges. </strong>   </span> We often switch on the Anti-alias option by default as developers if we are creating a Paint objec T. however, This option is really makes sense for vector objects. For bitmaps, this is used to blend the rectangular edges if it's rotated or skewed and it has no impact if the edge pixel S is transparent (as we would imagine most watch face arms would is). The hand on the left below have Anti-alias switched on, and the one on the right has it switched off. So turn off anti-aliasing for bitmaps to gain performance BACK. For our Watch face, we gained another 2 fps (fps up 11%) by switching this option off. </li> </ul> </ul> </ul> <ul> <ul> <li><span style="color: #ff0000;"><strong>Switch on Filterbitmap for all bitmap objects which</strong></span> is on top of other objects-this option smooths the edges W Hen Drawbitmap is Called. This should is confused with the filter option on Bitmap.createscaledbitmap for resizing bitmaps. We need both to is turned On. The bitmaps below is the magnified view of Santa ' s hand. The one on the left have Filterbitmap switched off and the one on the right have Filterbitmap switched On.</li> </ul> </ul>Eliminate expensive calls in the OnDraw loop (+3 Fps)<p><p>OnDraw is the most critical function call in watch Faces. It's called for every drawable frame, and the actual painting process cannot move forward until it ' s Finished. As such, our OnDraw method should is as light and as performant as Possible. Here's Some common problems that developers run into the can be avoided:</p></p> <ol type="1"> <ol type="1"> <li> <strong> do move heavy and common code to a precompute function </strong> &NBSP;-E.G. if we commonly grab r.array.clouddegrees, try doing that in onCreate, and just referencing it in the OnDraw loop. </li> <li> <strong> Don ' t repeat the same image transform in OnDraw </strong>  -it's common to resize bitmaps at runtime to fi t the screen size it is not a available in OnCreate. To avoid resizing the bitmap over and over again in onDraw, overrideonsurfacechanged where width and height informati On is available and resize images There. </li> <li> <strong> Don ' t allocate objects in OnDraw </strong>  -the leads to high memory churn which would force garbage coll Ection events to kick off, killing frame Rates. </li> <li> <strong> do analyze the CPU performance by using a tool such as the Android Device Monitor. </strong>  it ' s important that the OnDraw execution time was short and occurs in a regular period. </li> </ol> </ol><p><p>Following these simple rules would improve rendering performance drastically.</p></p><p><p>In the first version, the Santa OnDraw routine have a rogue line:</p></p><pre class="pretty print prettyprint"><pre class="pretty print prettyprint"><span class="kwd">int<span class="pun">[]<span class="pln"><span class="pun">=<span class="pln"><br>Getresources<span class="pun">(). <span class="pln"> Getintarray<span class="pun">(<span class="pln">R<span class="pun">. <span class="pln"> Array<span class="pun">. <span class="pln"> Clouddegrees<span class="pun">);</span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></pre></pre><p><p>This loads an int array on every call from resources which is Expensive. By eliminating this, we gained another 3 fps (fps up 17%).</p></p>Sub-pixel Smooth Animation ( -2 Fps)<p><p>For those keeping count, we should be------is the end product? The reason is a limitation with CANVAS.DRAWBITMAP. Although this command takes left and top positioning settings as a float, the API actually only deals with integers if it is purely translational for backwards compatibility reasons. As a result, the cloud can only move in increments of a whole pixel resulting in janky animations. In order to being sub-pixel smooth, we actually need to draw and then rotate rather than have pre-rotate clouds which moves Towards Santa. This additional rotation costs US 2 fps. however, the effect is worthwhile as the animation are now sub-pixel Smooth.</p></p><p><p><strong>Before-fast but Janky and wobbly</strong></p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">for (int i = 0; i < mcloudbitmaps.length; i++) { float r = centerx-(timeelapsed/mcloudspeeds[i])% centerx; <c1> float x = CenterX + -1 * (r * (float) math.cos (math.toradians (clouddegrees[i] +)); Float y = centery- R * (float) math.sin (math.toradians (clouddegrees[i] +)); Mcloudfilterpaints[i].setalpha ((int) (r/centerx * 255)); Bitmap cloud = mcloudbitmaps[i]; Canvas.drawbitmap (cloud, x-cloud.getwidth ()/2, y-cloud.getheight ()/2, mcloudfilterpaints[i]);} </c1></pre></pre><p><p><strong>after-slightly slower but sub-pixel smooth</strong></p></p><pre class="brush:java;gutter:true;"><pre class="brush:java;gutter:true;">for (int i = 0; i < mcloudbitmaps.length; i++) { canvas.save (); Canvas.rotate (mclouddegrees[i], centerx, centery); float R = centerx-(timeelapsed/(mcloudspeeds[i]))% centerx; Mcloudfilterpaints[i].setalpha ((int) (r/centerx * 255)); Canvas.drawbitmap (mcloudbitmaps[i], centerx, centery-r, mcloudfilterpaints[i]); Canvas.restore ();}</pre></pre><p><p>  </p></p><p><p><em>Before:integer translation values Create janky, wobbly Animation. After:smooth sailing!</em></p></p>Quality on every wrist<p><p>The watch face was the most prominent UI element in Android Wear. As craftspeople, it is a responsibility to make it SHINE. Let's put quality on every wrist!</p></p><p><p>Android Developers Blog</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.