不久前我问了一个问题,看看我是否能够在路径中找到一对特定点;但是,这次我想知道是否有办法知道路径中的所有点? (我找不到这样做的方法,这很不幸,因为 Java 提供了一种方法来做到这一点,而不是 Android?)
我问这个的原因是因为我有多个几何图形,我想比较这些点以查看它们相交的位置。
感谢任何有用的回复
最佳答案
您可以使用 PathMeasure
来获取路径上任意点的坐标。例如这个简单的 fragment (我看到的 here )返回路径一半处的点的坐标:
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);
或者此代码段返回一个 FloaPoint
s 数组。该数组包含路径上 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;
}
在上面的代码 fragment 中,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;
}
}
引用文献:
stackoverflow
Animating an image using Path and PathMeasure – Android
关于java - 如何在 Android 中找到路径中的所有点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972780/