How do I find all the Points in a Path in Android?

后端 未结 2 731
后悔当初
后悔当初 2020-12-08 16:36

Awhile back I asked a question to see if I was able to find a pair of specific points in a path; however, this time I want to know if there is a way to know all points in a

相关标签:
2条回答
  • 2020-12-08 17:08

    If you have created a Path that means that in some point of your code, you know the exact (Geo)point. Why don't you put this point(s) on a ArrayList or something similar?

    So for example before doing:

    path.lineTo(point.x, point.y);
    

    you can do:

    yourList.add(point);
    path.lineTo(point.x, point.y);
    

    And later you can get all your points from the ArrayList. Note you that you can take advantage of the Enhanced For Loop Syntax of ArrayList that execute up to three times faster.

    0 讨论(0)
  • 2020-12-08 17:32

    You can use PathMeasure to get coordinates of arbitrary point on the path.For example this simple snippet(that I saw here) returns coordinates of the point at the half of the path:

    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);
    

    Or this snippet returns an array of FloaPoints.That array involves coordinates of 20 points on the path:

    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;
        }
    

    In above snippet,FloatPoint is a class that encapsulate coordinates of a point:

    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;
            }
        }
    

    References:
    stackoverflow
    Animating an image using Path and PathMeasure – Android

    0 讨论(0)
提交回复
热议问题