开发者

Android - Why doesn't my axis draw?

开发者 https://www.devze.com 2023-03-25 04:44 出处:网络
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.

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 = cen开发者_开发百科terX + (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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消