是否可以使用android.graphics.Path绘制点

时间:2013-02-04 21:52:49

标签: android

我正在编写基于FingerPaint API演示示例在屏幕上绘图的简单android应用程序。在演示图中,只有在手指在TOUCH_TOLERANCE定义的屏幕上移动了一定距离后才开始。即使用户没有移动手指,我也想绘制一个点。有可能吗?

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(0xFFAAAAAA);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        //mPath.lineTo(x + 1, y + 1); //quick fix
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

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

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }

2 个答案:

答案 0 :(得分:2)

是的,有可能。只需将 1 添加到 x y 喜欢这个

//Create a dot
path.setLastPoint(x, y);
x++;
path.lineTo(x, y);

答案 1 :(得分:1)

添加一个小圆圈看起来更好,但如果在路径中添加了很多圆圈,则绘制时需要更多时间:

path.addCircle(x, y, 1, Path.Direction.CCW);

因此,在点周围添加一个小矩形就可以了:

path.addRect(x - 0.5f, y - 0.5f, x + 0.5f, y + 0.5f, Path.Direction.CCW);