问题
Currently I'm drawing a arrow head to my canvas by doing the following:
mPaint.setStyle(Style.FILL);
float deltaX = this.mPoints[1].x - this.mPoints[3].x;
float deltaY = this.mPoints[1].y - this.mPoints[3].y;
float frac = (float) 0.1;
float point_x_1 = this.mPoints[3].x + (1 - frac) * deltaX + frac * deltaY;
float point_y_1 = this.mPoints[3].y + (1 - frac) * deltaY - frac * deltaX;
float point_x_2 = this.mPoints[1].x;
float point_y_2 = this.mPoints[1].y;
float point_x_3 = this.mPoints[3].x + (1 - frac) * deltaX - frac * deltaY;
float point_y_3 = this.mPoints[3].y + (1 - frac) * deltaY + frac * deltaX;
Path path = new Path();
path.setFillType(Path.FillType.EVEN_ODD);
path.moveTo(point_x_1, point_y_1);
path.lineTo(point_x_2, point_y_2);
path.lineTo(point_x_3, point_y_3);
path.lineTo(point_x_1, point_y_1);
path.lineTo(point_x_1, point_y_1);
path.close();
canvas.drawPath(path, mPaint);
The above works fine. The problem I have is that the direction of the arrow is always pointing to the top right corner of my screen.
MY QUESTION:
How can I modify, what I already have, to draw the arrow in the direction of of my onTouchEvent
?
It's also worth mentioning that I already get the direction in my onTouchEvent
that I pass to my onDraw
as shown below:
//Starting point X
float startX = mX; // MotionEvent.ACTION_DOWN ---> mX = event.getX();
//Starting point Y
float startY = mY; // MotionEvent.ACTION_DOWN ---> mY = event.getY();
//Move to X
float stopX = pX; // MotionEvent.ACTION_MOVE ---> pX = event.getX();
//Move to Y
float stopY = pY; // MotionEvent.ACTION_MOVE ---> pY = event.getY();
Using the above, when I draw a line, I would call:
canvas.drawLine(startX, startY, stopX, stopY, mPaint);
This will draw the line in the direction I want.
The problem is that canvas.drawPath
takes 2 fields path and paint where canvas.drawLine
takes 5 fields startX, startY, stopX, stopY, paint
.
Any advise would greatly be appreciated.
来源:https://stackoverflow.com/questions/53495688/draw-arrow-according-to-path