我已经在一个Android应用程序中创建了画布,您可以在其中画线。该点上的所有内容都可以使用,但是我想从最后一行的端点坐标开始换行。
我以为我可以将最后一行的结束坐标放到新行的下移事件中,但是那样第一行就没有坐标,否则就不知道从哪里开始
目前,我的代码就是这样,这仅使线条成行,而没有连接。这有可能吗?
protected float mStartX;
protected float mStartY;
protected float mx;
protected float my;
private void drawLine(android.graphics.Canvas canvas) {
float dx = Math.abs(mx - mStartX);
float dy = Math.abs(my - mStartY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
canvas.drawLine(mStartX, mStartY, mx, my, mPaint);
}
}
private void lineDrawEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDrawing = true;
mStartX = mx;
mStartY = my;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
invalidate();
break;
case MotionEvent.ACTION_UP:
isDrawing = false;
mCanvas.drawLine(mStartX, mStartY, mx, my, mPaintFinal);
invalidate();
break;
}
}
答案 0 :(得分:2)
是的,您可以按照自己所说的那样存储最后一行的终点,用作下一行的起点。
如果您已经有一个起点,那么您需要做的就是改变ACTION_DOWN
的行为
类似:-
protected float mStartX;
protected float mStartY;
protected float mx;
protected float my;
private void drawLine(android.graphics.Canvas canvas) {
float dx = Math.abs(mx - mStartX);
float dy = Math.abs(my - mStartY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
canvas.drawLine(mStartX, mStartY, mx, my, mPaint);
}
}
private void lineDrawEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isDrawing = true;
// If this is the first line then the start values will be
// uninitialised, so only use them for the start of the first line
// Otherwise they will be set by the previous UP event
if (mStartX == null && mStartY == null) {
mStartX = mx;
mStartY = my;
}
invalidate();
break;
case MotionEvent.ACTION_MOVE:
invalidate();
break;
case MotionEvent.ACTION_UP:
isDrawing = false;
mCanvas.drawLine(mStartX, mStartY, mx, my, mPaintFinal);
// Set the Start point of the next line to end of current
mStartX = mx;
mStartY = my;
invalidate();
break;
}
}
您可能还希望将每个点也添加到“配对对象列表”中,以便以后可以对这些行进行处理。