Android - 为什么我的轴不绘制?

发布于 2024-11-28 06:22:59 字数 1861 浏览 1 评论 0原文

下面应该在屏幕中间绘制一个轴。然而,什么也没有出现。我确信这与我的道路有关。

@Override
protected void onDraw(Canvas canvas) {

    //Variables declared here temporarily for testing purposes
    int canterX = getWidth() /2;
    int centerY = getHeight() /2;
    int radius = 150;    

    Path verticalAxis = new Path();
    Path horizontalAxis = new Path();

    drawAxis();
}

private void drawAxis(Canvas canvas) {
    int axisLineThickness = 1;
    int verticalEndX;
    int verticalEndY;
    int horizontalEndX;
    int horizontalEndY;

    Paint axisPaint = new Paint();
    axisPaint.setColor(Color.WHITE);
    axisPaint.setStrokeWidth(axisLineThickness);

    double theta;

    for(int i = 90; i < 360; i += 180) {
        theta = toRadians(i);
        verticalEndX = centerX + (int) ((cos(theta)) * radius);
        verticalEndY = centerY + (int) ((sin(theta)) * radius);
        verticalAxis.moveTo(centerX, centerY);
        verticalAxis.lineTo(verticalEndX, verticalEndY);   
   }    
   canvas.drawPath(verticalAxis, axisColor);

   for(int i = 90; i < 360; i += 180) {
        theta = toRadians(i);
        horizontalEndX = centerX + (int) ((cos(theta)) * radius);
        horizontalEndY = centerY + (int) ((sin(theta)) * radius);
        horizontalAxis.moveTo(centerX, centerY);
        horizontalAxis.lineTo(verticalEndX, verticalEndY);   
   }    
   canvas.drawPath(horizontalAxis, axisColor);

}

我知道如果我分别将以下内容添加到垂直和水平 for 循环,我可以绘制轴:

Vertical For Loop:

canvas.drawLine(centerX, centerY, verticalEndX, verticalEndY, paint);

Horizo​​ntal For Loop:

canvas.drawLine(centerX, centerY, horizontalEndX, horizontalEndY, paint); 

但我不想以这种方式解决问题,我想纠正错误与我的道路。谁能告诉我为什么这些点没有正确添加到我的路径中?循环应该只经过两次,这会为轴的每一侧创建一条线。 IE。一个循环创建垂直轴的顶部,第二个循环创建底部。

如何让我的路径创建整条线,然后将其绘制到循环之外?

The following is supposed to draw an axis in the middle of the screen. However, nothing appears. I am positive that is has to do with my Paths.

@Override
protected void onDraw(Canvas canvas) {

    //Variables declared here temporarily for testing purposes
    int canterX = getWidth() /2;
    int centerY = getHeight() /2;
    int radius = 150;    

    Path verticalAxis = new Path();
    Path horizontalAxis = new Path();

    drawAxis();
}

private void drawAxis(Canvas canvas) {
    int axisLineThickness = 1;
    int verticalEndX;
    int verticalEndY;
    int horizontalEndX;
    int horizontalEndY;

    Paint axisPaint = new Paint();
    axisPaint.setColor(Color.WHITE);
    axisPaint.setStrokeWidth(axisLineThickness);

    double theta;

    for(int i = 90; i < 360; i += 180) {
        theta = toRadians(i);
        verticalEndX = centerX + (int) ((cos(theta)) * radius);
        verticalEndY = centerY + (int) ((sin(theta)) * radius);
        verticalAxis.moveTo(centerX, centerY);
        verticalAxis.lineTo(verticalEndX, verticalEndY);   
   }    
   canvas.drawPath(verticalAxis, axisColor);

   for(int i = 90; i < 360; i += 180) {
        theta = toRadians(i);
        horizontalEndX = centerX + (int) ((cos(theta)) * radius);
        horizontalEndY = centerY + (int) ((sin(theta)) * radius);
        horizontalAxis.moveTo(centerX, centerY);
        horizontalAxis.lineTo(verticalEndX, verticalEndY);   
   }    
   canvas.drawPath(horizontalAxis, axisColor);

}

I know I can make the axis draw if I add the following to the vertical and horizontal for loops respectively:

Vertical For Loop:

canvas.drawLine(centerX, centerY, verticalEndX, verticalEndY, paint);

Horizontal For Loop:

canvas.drawLine(centerX, centerY, horizontalEndX, horizontalEndY, paint); 

But I don't want to solve the issue this way, I want to correct what is wrong with my paths. Can anyone tell me why the points aren't adding to my path correctly? The loop should only go through twice which creates a line for each side of the axis. Ie. One loop creates the top of the vertical axis and the second loop creates the bottom part.

How do I get my paths create that full line and then draw it outside of the loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2024-12-05 06:22:59

Paint 的默认样式似乎是 FILL,所以也许路径中只有一条线会让人感到困惑。尝试将其设置为 STROKE:

axisPaint.setStyle(Paint.Style.STROKE);

请参阅 Paint.Style

Paint's default style appears to be FILL, so maybe just having a line in your path is confusing things. Try setting it to STROKE:

axisPaint.setStyle(Paint.Style.STROKE);

See Paint.Style

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文