在 android 中动态更新相同的 canvas
Dynamically updating the same canvas in android
我正在构建一个应用程序以在用户行走时绘制地图结构。为此,我使用 Sensor.TYPE_STEP_DETECTOR。在检测到每一步后,我调用以下方法 'setCordinates(StepCalculator stepCalculator)'。但是当我这样做时,只显示当前位置(一个圆圈),不显示到达那里的路径(以前的圆圈)。
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();
}
}
一条建议:
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);
}
};
然后,在每个新步骤中,在行 line.invalidate();
上调用 invalidade
我正在构建一个应用程序以在用户行走时绘制地图结构。为此,我使用 Sensor.TYPE_STEP_DETECTOR。在检测到每一步后,我调用以下方法 'setCordinates(StepCalculator stepCalculator)'。但是当我这样做时,只显示当前位置(一个圆圈),不显示到达那里的路径(以前的圆圈)。
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();
}
}
一条建议:
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);
}
};
然后,在每个新步骤中,在行 line.invalidate();