android学习18——对PathMeasure中getPosTan的理解

考虑这样的场景:要实现物体沿直接或曲线运动的效果.这就要算出某个时刻t,物体的坐标.getPosTan就是用来求坐标的.看下面的代码:

float step = 0.0001f;
Path path = new Path();
path.moveTo(100, 600);
path.cubicTo(400, 200, 700, 300, 800, 700);
PathMeasure measure = new PathMeasure(path, false);
float len = measure.getLength();
float[] point = new float[2];
for (float t = 0; t <= 1; t += step) {
    float dis = t * len;
    measure.getPosTan(dis, point, null);
    canvas.drawPoint(point[0], point[1], paint);
}

path定义了一个bezier曲线.getPosTan(dis, point, null)得到在曲线长度为dis时的坐标. 让dis从0到measure.getLength(),就可以取到bezier曲线上所有点的坐标.
参考资料

  1. android官方api https://developer.android.com/reference/android/graphics/PathMeasure.html
  2. 倪明田,吴良芝. 计算机图形学. p210-216. 北京大学出版社. 2014.
  3. 贝塞尔曲线扫盲 http://www.html-js.com/article/1628
原文地址:https://www.cnblogs.com/zhouyang209117/p/5527782.html