我正在开发一款用于提高驾驶速度的应用。我需要根据速度和时间(秒)绘制图形等线条。时间值是x轴,速度值是y轴。我尝试了随机值,它的工作原理。但是,如果我为更新的值绘制它,它不起作用。试着帮助我。 提前谢谢!
class MyView extends View {
Paint paint = new Paint();
Path path = new Path();
float[] x=new float[30];
float[] y=new float[30];
int i=0;
public MyView(Context context) {
super(context);
while(i<10)
{
x[i]= seconds;
y[i]= Speed;
y[i]=(float) (0.262*y[i]); // this is for drawing my y-axis to certain range
System.out.println("Current seconds:"+x[i]);
System.out.println("Current speed:"+y[i]);
seconds+= 60; // update for every 60 seconds
i++;
}
}
public void onDraw (Canvas canvas) {
path.moveTo(x[0],y[0]);
for (int i = 1; i < 10; i++) {
path.lineTo(x[i],y[i]);
}
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
paint.setColor(0xFFFF5050);
paint.setAntiAlias(true);
System.out.println("Height:"+relativelayout5.getHeight());
canvas.translate(0,relativelayout5.getHeight()); // reset where 0,0 is located
canvas.scale(1,-1);
canvas.drawPath(path, paint);
paint.setShadowLayer(0, 0, 0, 0);
paint.setStrokeJoin(Paint.Join.ROUND);
}
}
i在MainActivity中使此视图无效。