猿问

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

我想在我的绘图应用程序中记录用户的输入。它可以画出任何他想要的东西,而不仅仅是直线形状。我怎样才能在一条路径上记录用户在绘制时所做的所有动作,例如,一个特定的圆圈?这是我的方法,其中有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;

    }```


白衣非少年
浏览 103回答 1
1回答

largeQ

只需存储 (x,y) 点并在必要时稍后“重绘”它们。对于 (x,y),我指的是event getX&getY值。将所有值存储在 中ArrayList,然后您可以使用存储的值重绘形状。基本上,我建议你按照以下几行做一些事情,创建一个点数组ArrayList<Pair<Float,Float>> points = new ArrayList<>();将触摸点存储在数组中public boolean onTouchEvent(MotionEvent event) {&nbsp; &nbsp; float touchX = event.getX();&nbsp; &nbsp; float touchY = event.getY();&nbsp; &nbsp; //respond to down, move and up events&nbsp; &nbsp; switch (event.getAction()) {&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_DOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawPath.moveTo(touchX, touchY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points.add(new Pair<Float, Float>(touchX, touchY));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_MOVE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawPath.lineTo(touchX, touchY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points.add(new Pair<Float, Float>(touchX, touchY));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case MotionEvent.ACTION_UP:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawPath.lineTo(touchX, touchY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(touchX +", " +touchY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points.add(new Pair<Float, Float>(touchX, touchY));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //At this point you might want to&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //store this array somewhere&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //so you can use it to redraw later if needed&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawCanvas.drawPath(drawPath, drawPaint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawPath.reset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; //redraw&nbsp; &nbsp; invalidate();&nbsp; &nbsp; return true;}您可以在需要时使用数组重绘形状public void drawFromArrayList(ArrayList<Pair<Float,Float>> points) {&nbsp; &nbsp; int pointCount = points.size();&nbsp; &nbsp; if (pointCount < 2) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; for (int i=0;i<pointCount;i++) {&nbsp; &nbsp; &nbsp; &nbsp; float touchX = points.get(i).first, touchY = points.get(i).second;&nbsp; &nbsp; &nbsp; &nbsp; if(i==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawPath.moveTo(touchX, touchY);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; drawPath.lineTo(touchX, touchY);&nbsp; &nbsp; &nbsp; &nbsp; if(i==pointCount-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawCanvas.drawPath(drawPath, drawPaint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drawPath.reset();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答