Search code examples
javaandroidarraysdraw

coordinate X Y draw Android


I programmed a drawing application, I want to retrieve all the X Y of my drawing. That is to say each time I touch the screen, the coordinates x and y I put them in a two dimensional table ,

I made a toast to find out when the coordinates change, and I found that they change in the movetouch method, so I declare a table in the method and I still make a toast to see the 10 line Of my array, the toast changed co-ordination so I understood that in fact values ​​are crushed whenever the x and y change, or I am planting

 public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                   startTouch(x, y);
                    invalidate();
                    break;

            case MotionEvent.ACTION_UP:

                    upTouch();
                    invalidate();
                    break;

            case MotionEvent.ACTION_MOVE:

                    moveTouche(x, y);
                    invalidate();
                    break;

        }
        return true;
    }

Method moveTouch

  public void moveTouche (float x,float y ) {
    if ((canDraw)&& drawing) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if(dx >= Tolerance || dy >= Tolerance){
        path.quadTo(mX,mY,(x+mX)/2,(y+mY)/2);
        mX = x ;
        mY = y;
        double[][]  point = new double [99][2];
        for (int i = 0; i < 99; i++) {

            point[i][0]=x;
            point[i][1]=y;


        }
        Toast.makeText(getContext(),"y = "+point[10][1]+" ",Toast.LENGTH_LONG).show();

    }}
}

enter image description here


Solution

  • 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.