我正在尝试为我的应用程序创建一个基本图表,但我遇到了一个小问题。目前,发生的事情是我的图表是如我所愿生成的,但是当我在其中绘制我的点时,这些点总是“偏离 1”,具体来说,我的点应该位于第 15 位图表显示为 14。我的图表中的所有其他点也是如此,因为它们的显示也比应有的低 1。我尝试将 1 添加到 cond,但这似乎并没有改变任何实际显示的点,只是将我的标签在 y 轴上移动以开始更高。以下是我认为负责的代码,但如果需要更多,只需询问,我将发布所询问的内容。
注意:这是用于生成 2d 图形的课程作业,所以我不能专门使用图形课程,尽管我知道这样做要容易得多。
代码如下:
public class GraphicsView extends View {
ArrayList<Point> points = new ArrayList<Point>();
Path xPath = new Path();
Path yPath = new Path();
Path zPath = new Path();
Path drawGrid = new Path();
...
Integer xAxis;
Integer yAxis;
@Override
protected void onDraw(Canvas canvas) {
Integer counter = points.size();
Integer xCord;
super.onDraw(canvas);
yAxis = this.getHeight();
xAxis = this.getWidth();
canvas.drawColor(R.color.Background);
<Setting different paint objects to the right colors here>
xPath.moveTo(20, yAxis-20);
yPath.moveTo(20, yAxis-20);
zPath.moveTo(20, yAxis-20);
drawGrid.moveTo(22, yAxis-20);
drawGrid.lineTo(22, 0);
canvas.drawPath(drawGrid,grid);
drawGrid.moveTo(22, yAxis-20);
drawGrid.lineTo(xAxis, yAxis-22);
canvas.drawPath(drawGrid,grid);
//Left right Axis (X)
for (Integer ctr = counter; ctr > 0; ctr--) {
Integer value = counter- ctr + 1;
canvas.drawText(value.toString(), (xAxis-20)/ctr,yAxis-5, grid);
}
//Up down Axis (Y)
for (Integer ctr = 1; ctr < 22; ctr++) {
Integer value = ctr - 1;
canvas.drawText(value.toString(), 5, scale(yAxis, ctr), grid);
}
for (Point point : points)
{
xCord = ((xAxis -20) / counter);
xPath.lineTo(xCord, scale(yAxis,point.getxCord()));
xPath.addCircle(xCord, scale(yAxis, point.getxCord()), 2, Direction.CW);
yPath.lineTo(xCord, scale(yAxis, point.getyCord()));
yPath.addCircle(xCord, scale(yAxis,point.getyCord()), 2, Direction.CW);
zPath.lineTo(xCord, scale(yAxis, point.getzCord()));
zPath.addCircle(xCord, scale(yAxis, point.getzCord()), 2, Direction.CW);
canvas.drawPath(xPath, xPaint);
canvas.drawPath(yPath, yPaint);
canvas.drawPath(zPath, zPaint);
counter -= 1;
}
}
private Integer scale(Integer Axis, Integer cord) {
Integer point = 0;
point = (Axis - (((Axis-20)/21)) * cord);
return point;
}
}
重申一下,我正在寻找一种解决它的方法,以便当一个值设置为 15 时,它显示它沿着 y 轴上的 15。目前显示为14左右。