Is possible to attach an animator to a path? Is there any other way to draw on canvas animated lines? I searched this before i post , but there is nothing about this. In two other posts here and here there are walk around solutions and does not fit to me. Thank you!
I post my code inside the onDraw method to specify what exactly i want.
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
Path path = new Path();
path.moveTo(10, 50); // THIS TRANSFORMATIONS TO BE ANIMATED!!!!!!!!
path.lineTo(40, 50);
path.moveTo(40, 50);
path.lineTo(50, 40);
// and so on...
canvas.drawPath(path, paint);
Any ideas????
You can transform your canvas by time, i.e:
You can create a
PathMeasure
for your path and determine the length of it:You can then get get a specific point on the path using getPosTan() inside, say, a ValueAnimator's update listener:
You can then make use of the position in onDraw (or whatever).
The advantages of this approach is that it is straightforward, doesn't require chunky maths, and works on all APIs.
If using API 21+, you can use a ValueAnimator and pass in a Path to use its positions, which is simpler. Example SO question.
try this:
to test it add in your onCreate method: