Android:尝试绘制多条路径,但改为获得一条长路径

时间:2013-09-23 20:45:03

标签: android path drawing android-canvas

毫无疑问,我正在做一些愚蠢的事,但我似乎无法发现它。我试图根据用户的触摸绘制路径。捕获这些单独触摸的代码是:

public boolean onTouch(View view, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // The user is starting a new path
            Path path = new Path();
            paths.push(path);
            path.moveTo(x,y);
            path.lineTo(x,y);
            break;
        case MotionEvent.ACTION_MOVE:
            // The user is drawing on the current path
            path = paths.getLast();
            path.quadTo(x,y, (lastX + x)/2f, (lastY + y)/2f);
            break;
    }

    lastX = x;
    lastY = y;

    invalidate();
    return true;
}

然后绘制线条:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    ListIterator<Path> iterator = paths.listIterator();
    while (iterator.hasNext()) {
        Path path = iterator.next();
        canvas.drawPath(path, paint);
    }
}

问题是,当我开始绘制第二个(或第n个)路径时,代码在最后一个端点和当前端点之间添加一条线。有什么建议吗?

由于

1 个答案:

答案 0 :(得分:0)

我创建了一个DrawCanvas应用程序。你可以下载它here并检查它是如何工作的。