在Android中手指触摸后在画布上绘制线条

时间:2018-07-27 13:30:09

标签: android

我正在尝试在画布上绘制不同的形状,除直线以外的所有形状都可以成功。我没有得到如何沿着手指路径绘制直线。我的实现是

public GraffitiModel getModel() {
        return graffitiModel;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(imageToEdit!=null) {

            can.drawColor(0x323232);                                                                               
        }
        else{
            can.drawColor(0xdcdcdc);                                                                               
        }
        super.onDraw(can);
Paint paint= new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        brush.set(paint);


            brush.setColor(Color.BLACK);
            brush.setStrokeWidth((int)currentLine.girth);
            canvas.drawLine(currentLine.startX, currentLine.startY, currentLine.endX, currentLine.endY, brush);

if(shapeType == ShapeTypes.RECTANGLE){

                canvas.drawRect(currentLine.startX-50, currentLine.startY+50, currentLine.endX + 50, currentLine.endY-50, brush);
            }

             else if(shapeType==ShapeTypes.LINE) {

                for(Path p: paths) {
                    canvas.drawPath(p, brush);
                }
                }



        }

    }

    private float mX, mY;
    private static final float TOUCH_TOLEARNACE=3;

    private void touchStart(float x, float y) {
        path.reset();
        path.moveTo(x, y);
        mX=x;
        mY=y;
    }

    private void touchMove(float x, float y) {
        float dx= Math.abs(x-mX);
        float dy= Math.abs(y-mY);
        if(dx >= TOUCH_TOLEARNACE || dy >= TOUCH_TOLEARNACE) {
            mX=x;
            mY=y;
        }
    }

    private void touchUp() {
        path.lineTo(mX, mY);
        can.drawPath(path, brush);
        paths.add(path);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {


        switch(event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:

            if(!drawing) {

            drawingPointerId=event.getPointerId(0);
            drawing= true;
            lastXPos= (int) event.getX(0);
            lastYPos=(int) event.getY(0);
            if(shapeType==ShapeTypes.LINE) {
                touchStart(lastXPos, lastYPos);
            }



            }

            break;

        case MotionEvent.ACTION_MOVE:


        if(drawing) {

            final int pIdx=event.findPointerIndex(drawingPointerId);

            if(pIdx!=-1) {


                lastXPos= (int) event.getX(0);
                lastYPos=(int) event.getY(0);
                if(shapeType==ShapeTypes.LINE) {
                    touchMove(lastXPos, lastYPos);
                    invalidate();
                }
                postInvalidate();
            }

            }
            break;
        case MotionEvent.ACTION_UP:
        touchUp();
        invalidate();
        break;

        case MotionEvent.ACTION_POINTER_UP:
            if(drawing) {
                final int pIdxx = event.findPointerIndex(drawingPointerId);
                if (pIdxx != -1) {
                    lastXPos = -1;
                    lastYPos = -1;
                    drawing=false;  
                }
            }
            break;
        case MotionEvent.ACTION_CANCEL:
            lastXPos = -1;
            lastYPos = -1;
            drawing=false;
            break;  

        }
        return true;
    }

任何对此的帮助,我将能够绘制矩形圆和其他形状。但是我想沿着手指路径画直线。谢谢

0 个答案:

没有答案