-4

how to draw an arrow in android touch event?

i m trying to draw seperate straight line and an arrow head.how to set the arrowhead at the right position?

currentDrawingPath1.path.lineTo(motionEvent.getX()+5, motionEvent.getY());
currentDrawingPath1.path.lineTo(motionEvent.getX()-5, motionEvent.getY());
currentDrawingPath1.path.lineTo(motionEvent.getX(), motionEvent.getY());

above code is to draw a arrow head.

fabian
  • 80,457
  • 12
  • 86
  • 114
rams
  • 273
  • 1
  • 5
  • 13
  • How about some Trigonometry? – dmon Nov 26 '12 at 16:11
  • If i understand well your question, you want to draw an head arrow when, after dragging finger on screen, you remove finger. Well, you can add head arrow path to last x,y position registered when you remove you finger (from your event.ACTION_UP). Last problem is head arrow rotation according to line direction. For this, you have to calculate the angle of your line. It can be calculated by considering first and last touched point. Take a look here: http://stackoverflow.com/questions/2676719/calculating-the-angle-between-the-line-defined-by-two-points – kinghomer Nov 26 '12 at 17:26
  • Also: http://stackoverflow.com/questions/10316180/how-to-calculate-the-coordinates-of-a-arrowhead-based-on-the-arrow – dmon Nov 26 '12 at 18:31

1 Answers1

1

i tried this below code.this is worked for me,

enter code here


switch (event.getAction())
{
   case MotionEvent.ACTION_DOWN:
        mPath.reset();
    mPath.moveTo(x, y);
        mX = x;
        mY = y;
        startPoint = new PointF(event.getX(), event.getY());
        endPoint = new PointF();
        invalidate();

     //   isDrawing = true;
        break;
    case MotionEvent.ACTION_MOVE:
            float dx = Math.abs(x - mX);
        System.out.println("action move");
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
        {
        //  currentDrawingPath.path.quadTo(mX,mY,(x + mX)/2, (y + mY)/2);
        }
        mX = x;
        mY = y;
          endPoint.x = event.getX();
            endPoint.y = event.getY();
          isDrawing = true;

          invalidate();
        break;
    case MotionEvent.ACTION_UP:
           mPath.lineTo(mX, mY);
float deltaX =   endPoint.x-startPoint.x;
           float deltaY =   endPoint.y-startPoint.y;
           float frac = (float) 0.1;

           float point_x_1 = startPoint.x + (float) ((1 - frac) * deltaX + frac * deltaY);
           float point_y_1 = startPoint.y + (float) ((1 - frac) * deltaY - frac * deltaX);

           float point_x_2 = endPoint.x;
           float point_y_2 = endPoint.y;

           float point_x_3 = startPoint.x + (float) ((1 - frac) * deltaX - frac * deltaY);
           float point_y_3 = startPoint.y + (float) ((1 - frac) * deltaY + frac * deltaX);



           mPath.moveTo(point_x_1, point_y_1);
           mPath.lineTo(point_x_2, point_y_2);
           mPath.lineTo(point_x_3, point_y_3);
           mPath.lineTo(point_x_1, point_y_1);
           mPath.lineTo(point_x_1, point_y_1);

            mCanvas.drawPath(mPath, ppaint);
            endPoint.x = event.getX();
            endPoint.y = event.getY();
            isDrawing = false;

            invalidate();
        break;
    default:
        break;
}       
rams
  • 273
  • 1
  • 5
  • 13