Android平滑多点触控绘图

时间:2012-08-15 09:09:47

标签: java android drawing multi-touch

我正在尝试制作一个简单的绘图应用。我喜欢平滑的手指绘制简单的曲线。并使用多点触控。我跟随代码:

@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;
}

还有其他方式吗?我怎么能用三根手指画得很漂亮?

2 个答案:

答案 0 :(得分:1)

MotionEvent为此类应用程序嵌入了历史信息。在批处理标题下的http://developer.android.com/reference/android/view/MotionEvent.html有一个很好的例子

答案 1 :(得分: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;
}