简体   繁体   English

穿戴OS动画Android

[英]Wear OS animation Android

How can i animate a line on a canvas in WearOS every 5 seconds? 如何每5秒钟在WearOS为画布上的line设置动画?

I know that we have to use an AnimatorTask with a call to postInvalidate() . 我知道我们必须使用AnimatorTask并调用postInvalidate() However since I am directly drawing the line on the canvas , I do not have a View object. 但是,由于我是直接在canvas上绘制line ,所以没有View对象。

public class AnimatorTask extends TimerTask {

    private WatchEventInfo eventInfo;

    public AnimatorTask(WatchEventInfo eventInfo) {
        this.eventInfo = eventInfo;
    }

    @Override
    public void run() {
        drawAndAnimate();
    }

    public WatchEventInfo getEventInfo() {
        return eventInfo;
    }

    private void drawAndAnimate() {

        Canvas canvas = eventInfo.getCanvas();

        // For testing
        canvas.drawLine(100, 100, 300, 300 markerPaint);
    }
}

How do get access to the canvas.drawLine() method object and notify the canvas to redraw itself from the TimerTask assuming that my TimerTask exists in CanvasWatchFaceService subclass? 假设我的TimerTask存在于CanvasWatchFaceService子类中,如何访问canvas.drawLine()方法对象并通知canvasTimerTask重绘自身?

You get access to the canvas by overriding the onDraw(Canvas canvas, Rect bounds) method declared in the CanvasWatchFaceService.Engine class. 通过重写CanvasWatchFaceService.Engine类中声明的onDraw(Canvas canvas, Rect bounds)方法onDraw(Canvas canvas, Rect bounds)可以访问画布。

If you create your watch face using the New Project wizard in Android Studio, you'll notice a method at the end of the MyWatchFace class called handleUpdateTimeMessage() . 如果使用Android Studio中的“新建项目”向导创建表盘,则会在MyWatchFace类末尾看到一个名为handleUpdateTimeMessage() This logic will ensure that your onDraw method gets called at the interval specified by INTERACTIVE_UPDATE_RATE_MS (default is once per second) in interactive mode. 此逻辑将确保在交互模式下以INTERACTIVE_UPDATE_RATE_MS指定的间隔(默认为每秒一次)调用onDraw方法。

Add a check in your onDraw method to only trigger your line animation if five seconds have past since the last update. 在onDraw方法中添加一个检查,以仅在自上次更新以来已过了五秒钟才触发线条动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM