Reputation: 9152
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?
Upvotes: 1
Views: 453
Reputation: 4784
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.
Upvotes: 2