這裏有一個很好的例子,用於複製粘貼。只需創建擴展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;
}
那麼你現在的代碼會發生什麼?你還需要記住,有很多不同的設備,並不是所有的處理多點觸摸同樣好。 – Warpzit 2012-08-15 09:13:37
@Warpzit它繪製了我用手指畫的東西,但它很慢。我認爲這是因爲添加了太多路徑到數組列表。但是,如果我清除它。它會刪除我以前的曲線。我應該將它們轉換成任何東西嗎 我想用這個應用程序來滿足我的需求。我的手機支持多點觸控,所以我想使用它。 – sczdavos 2012-08-15 09:20:12
不要因爲某事而認爲它,你需要測試它。嘗試添加打印出路徑長度的logcat。並且不要在調試模式下運行它,因爲它會使默認情況下運行緩慢:) – Warpzit 2012-08-15 09:22:32