简体   繁体   中英

Wear OS animation Android

How can i animate a line on a canvas in WearOS every 5 seconds?

I know that we have to use an AnimatorTask with a call to postInvalidate() . However since I am directly drawing the line on the canvas , I do not have a View object.

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?

You get access to the canvas by overriding the onDraw(Canvas canvas, Rect bounds) method declared in the CanvasWatchFaceService.Engine class.

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() . 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.

Add a check in your onDraw method to only trigger your line animation if five seconds have past since the last update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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