根据路径绘制箭头

时间:2018-11-27 08:43:20

标签: android android-canvas android-touch-event

目前,我正在通过以下操作在我的画布上绘制一个箭头:

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

以上工作正常。我的问题是箭头的方向始终指向屏幕的右上角。

enter image description here


我的问题:

如何修改已经拥有的东西,以朝着onTouchEvent的方向绘制箭头?


值得一提的是,我已经在onTouchEvent中获得了指向onDraw的指示,如下所示:

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

使用以上内容,当我画一条线时,我会呼叫:

canvas.drawLine(startX, startY, stopX, stopY, mPaint);

这将沿我想要的方向画线。 问题在于,canvas.drawPath采用2字段路径并绘制,而canvas.drawLine采用5字段startX, startY, stopX, stopY, paint

任何建议将不胜感激。

0 个答案:

没有答案