为了账号安全,请及时绑定邮箱和手机立即绑定

记录并存储曲线形状的路径

记录并存储曲线形状的路径

白衣非少年 2023-04-13 15:39:47
我想在我的绘图应用程序中记录用户的输入。它可以画出任何他想要的东西,而不仅仅是直线形状。我怎样才能在一条路径上记录用户在绘制时所做的所有动作,例如,一个特定的圆圈?这是我的方法,其中有System.out.println()是我将推送方法以保存路径的地方。    public boolean onTouchEvent(MotionEvent event) {        float touchX = event.getX();        float touchY = event.getY();        //respond to down, move and up events        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                drawPath.moveTo(touchX, touchY);                break;            case MotionEvent.ACTION_MOVE:                drawPath.lineTo(touchX, touchY);                break;            case MotionEvent.ACTION_UP:                drawPath.lineTo(touchX, touchY);                System.out.println(touchX +", " +touchY);                drawCanvas.drawPath(drawPath, drawPaint);                drawPath.reset();                break;            default:                return false;        }        //redraw        invalidate();        return true;    }```
查看完整描述

1 回答

?
largeQ

TA贡献2039条经验 获得超7个赞

只需存储 (x,y) 点并在必要时稍后“重绘”它们。


对于 (x,y),我指的是event getX&getY值。将所有值存储在 中ArrayList,然后您可以使用存储的值重绘形状。基本上,我建议你按照以下几行做一些事情,


创建一个点数组


ArrayList<Pair<Float,Float>> points = new ArrayList<>();

将触摸点存储在数组中


public boolean onTouchEvent(MotionEvent event) {

    float touchX = event.getX();

    float touchY = event.getY();

    //respond to down, move and up events

    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            drawPath.moveTo(touchX, touchY);

            points.add(new Pair<Float, Float>(touchX, touchY));

            break;

        case MotionEvent.ACTION_MOVE:

            drawPath.lineTo(touchX, touchY);

            points.add(new Pair<Float, Float>(touchX, touchY));

            break;

        case MotionEvent.ACTION_UP:

            drawPath.lineTo(touchX, touchY);

            System.out.println(touchX +", " +touchY);


            points.add(new Pair<Float, Float>(touchX, touchY));

            //At this point you might want to

            //store this array somewhere 

            //so you can use it to redraw later if needed


            drawCanvas.drawPath(drawPath, drawPaint);

            drawPath.reset();

            break;

        default:

            return false;

    }

    //redraw

    invalidate();

    return true;

}

您可以在需要时使用数组重绘形状


public void drawFromArrayList(ArrayList<Pair<Float,Float>> points) {

    int pointCount = points.size();

    if (pointCount < 2) {

        return;

    }

    for (int i=0;i<pointCount;i++) {

        float touchX = points.get(i).first, touchY = points.get(i).second;

        if(i==0) {

            drawPath.moveTo(touchX, touchY);

        }

        drawPath.lineTo(touchX, touchY);

        if(i==pointCount-1) {

            drawCanvas.drawPath(drawPath, drawPaint);

            drawPath.reset();

        }

    }

}


查看完整回答
反对 回复 2023-04-13
  • 1 回答
  • 0 关注
  • 82 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信