has*_*ian 32
您可以使用PathMeasure获取路径上任意点的坐标.例如,这个简单的片段(我在这里看到)返回路径一半的点的坐标:
PathMeasure pm = new PathMeasure(myPath, false);
//coordinates will be here
float aCoordinates[] = {0f, 0f};
//get point from the middle
pm.getPosTan(pm.getLength() * 0.5f, aCoordinates, null);
Run Code Online (Sandbox Code Playgroud)
或者这个片段返回一个FloaPoints数组.该数组涉及路径上20个点的坐标:
private FloatPoint[] getPoints() {
FloatPoint[] pointArray = new FloatPoint[20];
PathMeasure pm = new PathMeasure(path0, false);
float length = pm.getLength();
float distance = 0f;
float speed = length / 20;
int counter = 0;
float[] aCoordinates = new float[2];
while ((distance < length) && (counter < 20)) {
// get point from the path
pm.getPosTan(distance, aCoordinates, null);
pointArray[counter] = new FloatPoint(aCoordinates[0],
aCoordinates[1]);
counter++;
distance = distance + speed;
}
return pointArray;
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码片段中,FloatPoint是一个封装点坐标的类:
class FloatPoint {
float x, y;
public FloatPoint(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}
Run Code Online (Sandbox Code Playgroud)
参考:
stackoverflow
使用Path和PathMeasure动画图像 - Android
如果您创建了 ,Path这意味着在代码的某些点,您知道确切的(地理)点。你为什么不把这一点放在一个ArrayList或类似的东西上?
例如,在执行之前:
path.lineTo(point.x, point.y);
Run Code Online (Sandbox Code Playgroud)
你可以做:
yourList.add(point);
path.lineTo(point.x, point.y);
Run Code Online (Sandbox Code Playgroud)
稍后您可以从 中获得所有积分ArrayList。请注意,您可以利用增强型 For 循环语法,其ArrayList执行速度最多可提高三倍。