0

我正在尝试在 android 的地图上绘制一个形状。如果用户绘制封闭路径,则很好。如果用户绘制了一条开放路径,那么如何关闭路径以使其看起来更好?

请看图,

在此处输入图像描述

在这里,我画了一条开放的路径。

一旦 onTouch 动作为 ACTION_UP,路径就会自行关闭,

像这样,

在此处输入图像描述

有没有办法做到这一点?我已经对此进行了裁判,如何绘制闭合曲线形状?,但这对我没有帮助。

这是我的代码,

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    Log.d("","OnTouch");
    switch (action) {
    case MotionEvent.ACTION_DOWN:
      downx = event.getX();
      downy = event.getY();
      break;
    case MotionEvent.ACTION_MOVE:
        upx = event.getX();
          upy = event.getY();
          canvas.drawLine(downx, downy, upx, upy, paint);
          drawable.invalidate();
          downx=upx;
          downy=upy;
      break;
    case MotionEvent.ACTION_UP:

      break;
    case MotionEvent.ACTION_CANCEL:
      break;
    default:
      break;

    }
    return true;
  }

请给我建议。我并不完全了解这个画布绘图。

任何帮助表示赞赏。

4

2 回答 2

1

试试下面的方法。

public class MainActivity extends Activity {
    private ArrayList<Path> _graphics = new ArrayList<Path>();
    private Paint mPaint;
    float x1, x2, y1, y2;
    boolean isDraw;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new DrawingPanel(this));
        System.out.println(x1 + " " + x2 + " " + y1 + " " + y2);
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFF00);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);

    }

    class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
        private DrawingThread _thread;
        private Path path;

        public DrawingPanel(Context context) {
            super(context);
            getHolder().addCallback(this);
            _thread = new DrawingThread(getHolder(), this);
        }

        public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    path = new Path();
                    x1 = event.getX();
                    y1 = event.getY();
                    path.moveTo(event.getX(), event.getY());
                    path.lineTo(event.getX(), event.getY());
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    path.lineTo(event.getX(), event.getY());
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    x2 = event.getX();
                    y2 = event.getY();
                    path.lineTo(event.getX(), event.getY());
                    _graphics.add(path);
                    isDraw = true;
                }

                return true;
            }
        }

        @Override
        public void onDraw(Canvas canvas) {
            for (Path path : _graphics) {
                // canvas.drawPoint(graphic.x, graphic.y, mPaint);
                canvas.drawPath(path, mPaint);
                if (isDraw) {
                    canvas.drawLine(x1, y1, x2, y2, mPaint);
                    isDraw = false;
                }
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub

        }

        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            _thread.setRunning(true);
            _thread.start();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
        }
    }

    class DrawingThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private DrawingPanel _panel;
        private boolean _run = false;

        public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }

        public void setRunning(boolean run) {
            _run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                        _panel.onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}

我不知道这是否适用于地图。但这将适用于正常视图。试试这个方法。我得到了这个编码的以下输出。我希望这能帮到您。

在此处输入图像描述

于 2013-06-10T05:54:05.367 回答
1

这样解决,

public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    path = new Path();
    float upX;
    float upY;
    Log.d("", "OnTouch");
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        downx = event.getX();
        downy = event.getY();
        eventX = downx;
        eventY = downy;
        Log.d("", "startx" + eventX);
        break;
    case MotionEvent.ACTION_MOVE:
        upx = event.getX();
        upy = event.getY();
        canvas.drawLine(downx, downy, upx, upy, paint);
        drawable.invalidate();
        downx = upx;
        downy = upy;
        break;
    case MotionEvent.ACTION_UP:
        upX = event.getX();
        upY = event.getY();
        Log.d("", "Action Up");
        Log.d("", "endx" + upX);
        //path.moveTo(eventX, eventY);
        canvas.drawLine(eventX, eventY, upX, upY, paint);
        drawable.invalidate();
        return true;
    case MotionEvent.ACTION_CANCEL:
        break;
    default:
        break;

    }
    return true;
}
于 2013-06-10T06:06:12.907 回答