17

Is there a way to read the points created when drawing a path? It seems silly to me that a path cannot be readable.

Or is it just better to manually write the current finger position to an array?

thanks

Matt
  • 1,368
  • 4
  • 16
  • 32

3 Answers3

36

You can read as many points as you want from any path. Example how to read coordinates from the middle of path:

    PathMeasure pm = new PathMeasure(myPath, false);
    //coordinates will be here
    float aCoordinates[] = {0f, 0f};

    //get coordinates of the middle point
    pm.getPosTan(pm.getLength() * 0.5f, aCoordinates, null);

You can pass any distance from the path start to get point coordinates.

Eduard
  • 1,464
  • 15
  • 19
  • pm.getPosTan(pm.getLength() * 0.5f, aCoordinates, null); returns boolean value then wehre is the point ??? – SweetWisher ツ Sep 04 '13 at 12:28
  • 5
    @JhansiKiRani, he sais right there: //coordinates will be here. So in aCoordinates. – Florin Vistig Sep 12 '13 at 09:56
  • how do you get the points on a line that is drawn with a finger using this answers method? assume it isn't known but the start points are kept in `Path` – dan Nov 28 '20 at 12:32
  • @dan Let's say you want 11 points (10 equal segments): ``` // the begining: pm.getPosTan(pm.getLength() * 0.0f, aCoordinates, null); // 1st point: pm.getPosTan(pm.getLength() * 0.1f, aCoordinates, null); // 2nd point: pm.getPosTan(pm.getLength() * 0.2f, aCoordinates, null); // 3rd point: pm.getPosTan(pm.getLength() * 0.3f, aCoordinates, null); // etc // the last point pm.getPosTan(pm.getLength(), aCoordinates, null); ``` – Eduard Nov 29 '20 at 17:53
  • thanks for the reply. So then the coordinates in aCoordinates will be changed to the corresponding distance then? – dan Nov 30 '20 at 10:16
  • @dan Yes, `aCoordinates` will get coordinates of a corresponding point. This demo code uses a simple data type. It's up to you to use more appropriate type. And you're not limited to equal segments. – Eduard Dec 01 '20 at 17:08
1

As far as I know, I think that you can't get previously added points, but you can extend Path class and create your own, override add methods, and then store that points in an array or a list or whatever you prefer.

Rabas
  • 573
  • 1
  • 6
  • 13
  • Thanks, that’s what I thought. I don’t think extending the class will add anything more than just adding the points (x, y) from onMyTouchEvent to an ArrayList instead. – Matt Jul 06 '10 at 15:52
-1

You mentioned finger position in your question. If you are drawing and using motion events, you could add the X and Y positions to an ArrayList during the event where all even indices are X's and odds are Y's. I used this in a couple of drawing apps I created. To recreate the path all you need is a for loop and Path.lineTo().

Also if you have drawn the path to a view with a specific color, say Color.Black, you can use Bitmap.getPixels(...) and create an array {x0,y0,x1,y1,....xn,yn} based off a for loop like

int i = 0;
for(int y = 0; y < bitmap.getHeight(); y++){
    for(int x = 0; x < bitmap.getWidth(); x++){
        if(pixels[y*bitmap.getWidth()+x] == Color.BLACK){
            xy[i] = x;
            i++;
            xy[i] = y;
            i++;
        }
    }
}

The array xy has all your coordinates.

LawrenceBaker
  • 49
  • 1
  • 6