Search code examples
androidcanvasondraw

Dynamically updating the same canvas in android


Im building an application to draw a map structure when the user is walking. To do this i use Sensor.TYPE_STEP_DETECTOR. Upon every step detected i call the following method 'setCordinates(StepCalculator stepCalculator)'. But when i do that only the current position (one circle) is displayed, the path taken to come there(previous circles) are not shown.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import java.util.ArrayList;

public class DrawView extends View {
Paint paint = new Paint();
private int cx=0,cy=0;
private Step step;
private  ArrayList<Step> steps;
private Bitmap bm;

public DrawView(Context context) {
    super(context);
    paint.setColor(Color.BLACK);
    steps = new ArrayList<Step>();
    step = new Step();

}


@Override
public void onDraw(Canvas canvas) {





        for (Step i : steps) {
            cy = i.getY();
            cx = i.getX();
            paint.setColor(Color.RED);
            canvas.drawCircle(cx, cy, 50, paint);
            paint.setColor(Color.BLACK);
            canvas.drawCircle(cx, cy, 30, paint);
            this.invalidate();
       }



       // }


    /*for(int i =0;i<100;i++)
    {
        paint.setColor(Color.RED);
        canvas.drawCircle(50+(i*10), 50+(i*10), 50, paint);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(50+(i*10), 50+(i*10), 30, paint);
        this.invalidate();
    }*/




}


public void setCordinates(StepCalculator stepCalculator) {

    stepCalculator.calculateCordinates();
    step.setX(stepCalculator.getXCordinates());
    step.setY(stepCalculator.getYCordinates());
    steps.add(step);

    this.invalidate();


}

}


Solution

  • A suggestion:

    final Path mPath = new Path();
    final Paint mPaint = new Paint();
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeWidth(40);
                mPaint.setColor(
                        Color.RED
                );
                mPaint.setPathEffect(
                        new DashPathEffect(
                                new float[]{10, 10},
                                0
                        )
                );
    ImageView line = new ImageView(context) {
                    @Override
                    protected void onDraw(Canvas canvas) {
                        super.onDraw(canvas);
                            mPath.reset();
                            mPath.moveTo(
                                    YOUR_STEP_X,
                                    YOUR_STEP_Y
                            );
                            // If you are saving your steps, then cicle them here using lineTo()
                            mPath.lineTo(
                                    YOUR_NEW_STEP_X,
                                    YOUR_NEW_STEP_Y
                            );
                            canvas.drawPath(mPath, mPaint);
                    }
                };
    

    Then, on each new step, call invalidade on line line.invalidate();