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

> 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()  and Canvas.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 the setAntiAlias()  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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值