2013-09-23 80 views
1

毫無疑問,我正在做一些愚蠢的事情,但我似乎無法發現它。我試圖根據用戶的觸摸來繪製路徑。捕捉那些個體接觸的代碼是:Android:試圖繪製多條路徑,但得到一條長路徑代替

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個)的路徑,該代碼是在最後一個端點和當前開始端點之間添加一條線。有什麼建議麼?

謝謝

回答

0

我已經創建了一個DrawCanvas應用程序。你可以下載它here並檢查它是如何工作的。