2012-08-15 100 views
3

我正在嘗試製作一個簡單的繪圖應用程序。我想要光滑的手指繪製簡單的曲線。並使用多點觸控。我有以下代碼:Android平滑多點觸摸繪圖

@Override 
public void onDraw(Canvas canvas) { 
    for (Path path : paths) { 
     canvas.drawPath(path, paint); 
    } 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    float xPos = event.getX(); 
    float yPos = event.getY(); 

    if (event.getAction() == MotionEvent.ACTION_DOWN) 
    { 
     path = new Path(); 
     path.moveTo(xPos, yPos); 
    } 
    else if (event.getAction() == MotionEvent.ACTION_MOVE) 
    { 
     path.lineTo(xPos, yPos); 
        paths.add(path); 
    } 
    else if (event.getAction() == MotionEvent.ACTION_UP) 
    { 
     path.lineTo(xPos, yPos); 
        paths.add(path); 
    } 

    invalidate(); 

    return true; 
} 

是其他方式嗎?我怎樣才能用三根手指畫好,並且很平滑?

+0

那麼你現在的代碼會發生什麼?你還需要記住,有很多不同的設備,並不是所有的處理多點觸摸同樣好。 – Warpzit 2012-08-15 09:13:37

+0

@Warpzit它繪製了我用手指畫的東西,但它很慢。我認爲這是因爲添加了太多路徑到數組列表。但是,如果我清除它。它會刪除我以前的曲線。我應該將它們轉換成任何東西嗎 我想用這個應用程序來滿足我的需求。我的手機支持多點觸控,所以我想使用它。 – sczdavos 2012-08-15 09:20:12

+0

不要因爲某事而認爲它,你需要測試它。嘗試添加打印出路徑長度的logcat。並且不要在調試模式下運行它,因爲它會使默認情況下運行緩慢:) – Warpzit 2012-08-15 09:22:32

回答

1

MotionEvent爲此類應用程序嵌入了歷史信息。 http://developer.android.com/reference/android/view/MotionEvent.html在配方標題

+1

謝謝,它工作完美! 下面是使用任何其他誰有同樣的問題的好例子: http://corner.squareup.com/2010/07/smooth-signatures.html 對於多點觸控,我用event.getPointerCount();然後event.getX(pointer_index); – sczdavos 2012-08-15 11:33:02

+0

嗨,你是怎麼用多點觸控做到這一點的?需要「保存」繪製的點數?如果你能發佈這部分代碼,我真的很滿意 – 2013-11-09 00:59:44

1

這裏有一個很好的例子,用於複製粘貼。只需創建擴展View的類並實現以下方法即可。

private final Paint paint = new Paint(); // Don't forgot to init color, form etc. 

@Override 
protected void onDraw(Canvas canvas) { 
    for (int size = paths.size(), i = 0; i < size; i++) { 
     Path path = paths.get(i); 
     if (path != null) { 
      canvas.drawPath(path, paint); 
     } 
    } 
} 

private HashMap<Integer, Float> mX = new HashMap<Integer, Float>(); 
private HashMap<Integer, Float> mY = new HashMap<Integer, Float>(); 
private HashMap<Integer, Path> paths = new HashMap<Integer, Path>(); 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int maskedAction = event.getActionMasked(); 

    Log.d(TAG, "onTouchEvent"); 

    switch (maskedAction) { 
     case MotionEvent.ACTION_DOWN: 
     case MotionEvent.ACTION_POINTER_DOWN: { 
      for (int size = event.getPointerCount(), i = 0; i < size; i++) { 
       Path p = new Path(); 
       p.moveTo(event.getX(i), event.getY(i)); 
       paths.put(event.getPointerId(i), p); 
       mX.put(event.getPointerId(i), event.getX(i)); 
       mY.put(event.getPointerId(i), event.getY(i)); 
      } 
      break; 
     } 
     case MotionEvent.ACTION_MOVE: { 
      for (int size = event.getPointerCount(), i = 0; i < size; i++) { 
       Path p = paths.get(event.getPointerId(i)); 
       if (p != null) { 
        float x = event.getX(i); 
        float y = event.getY(i); 
        p.quadTo(mX.get(event.getPointerId(i)), mY.get(event.getPointerId(i)), (x + mX.get(event.getPointerId(i)))/2, 
          (y + mY.get(event.getPointerId(i)))/2); 
        mX.put(event.getPointerId(i), event.getX(i)); 
        mY.put(event.getPointerId(i), event.getY(i)); 
       } 
      } 
      invalidate(); 
      break; 
     } 
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_POINTER_UP: 
     case MotionEvent.ACTION_CANCEL: { 
      for (int size = event.getPointerCount(), i = 0; i < size; i++) { 
       Path p = paths.get(event.getPointerId(i)); 
       if (p != null) { 
        p.lineTo(event.getX(i), event.getY(i)); 
        invalidate(); 
        paths.remove(event.getPointerId(i)); 
        mX.remove(event.getPointerId(i)); 
        mY.remove(event.getPointerId(i)); 
       } 
      } 
      break; 
     } 
    } 

    return true; 
}