简体   繁体   中英

How to draw a navigation path using Canvas onDraw() in android java

I am new to java drawing and I would like to implement a navigation system so a user can navigate on a map. The java function 'onDraw' is implement so it shows the location of the user on the map and also show his movements. Below are my codes:

class CanvasView extends View {
        private Paint paint = new Paint();
        private Path path = new Path();
        private float lineX = Position.x_axis;
        private float lineY = Position.y_axis;
        private float lastX = Position.x_axis;
        private float lastY = Position.y_axis;

        public CanvasView(Context context) {
            super(context);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(10f);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            drawRotateImage(canvas);
            canvas.drawPoint(Position.x_axis, Position.y_axis, paint);

            Path path = new Path();
            path.moveTo(lastX, lastY);
            path.lineTo(lineX,lineY);
            canvas.drawPath(path, paint);
        }

        private void stepDetector (float step) {
            stepCount += (int) step;
            distance = stepCount * 30;
            lineX = (float) (lineX + 30);
            lineY = (float) (lineY + 30);
            invalidate();
            lastX = lineX;
            lastY = lineY;
        }
}

The stepdetector is called each time a step has been detected and it should repaint the canvas to show to movements. Noting that the previous movement line needs to be visible, that is it should be a continuous line.

I am having issues to get the navigation line path. It would be grateful if I can get some help on it.

Thank you in advance.

The onDraw draws a fresh copy of the contents everytime and does not remain old information. For what you are trying to draw, you can keep a list of type Position and add the new position to the list when stepDetector is called.

Then, inside onDraw , iterate over the list and draw lines connecting all the points from the list.

try this, make list and for every step add points, and in ondraw draw all points from list

class CanvasView extends View {
        List<Point> points;
        private Paint paint = new Paint();
        private Path path = new Path();
        private float lineX = Position.x_axis;
        private float lineY = Position.y_axis;
        private float lastX = Position.x_axis;
        private float lastY = Position.y_axis;

        public CanvasView(Context context) {
            super(context);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(10f);
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            points=new ArrayList<>();

        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            drawRotateImage(canvas);
            canvas.drawPoint(Position.x_axis, Position.y_axis, paint);
            // now here draw all points from list
            Path path = new Path();
            path.moveTo(lastX, lastY);
            path.lineTo(lineX,lineY);
            canvas.drawPath(path, paint);
        }

        private void stepDetector (float step) {
            stepCount += (int) step;
            distance = stepCount * 30;
            lineX = (float) (lineX + 30);
            lineY = (float) (lineY + 30);
            invalidate();
            lastX = lineX;
            lastY = lineY;
            // add every point to the list after step
        }

class Point{
float x,y;     

}

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM