2017-02-13 164 views
-1

我在屏幕的中间画了一个圆,我想从圆的中心向上向下绘制四条线a,向左和向右 ,问题是我绘制的链接我不能确定它们的长度绘制线的坐标

enter image description here

@Override 
protected void onDraw (Canvas canvas){ 
    super.onDraw(canvas); 
    int x = getWidth(); 
    int y = getHeight(); 
    int radius; 
    radius = 25; 
    Paint paint = new Paint() ; 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.BLACK); 
    canvas.drawPaint(paint); 
    paint.setColor(Color.WHITE); 
    canvas.drawCircle(x/2, y/2, radius, paint); 
    Paint paint1 = new Paint() ; 
    paint1.setStyle(Paint.Style.FILL); 
    paint1.setColor(Color.BLACK); 
    canvas.drawPaint(paint1); 
    paint1.setColor(Color.WHITE); 
    canvas.drawLine(x/2, y/2, x, y/2, paint1); 
    canvas.drawLine(x/2, y/2, -x, y/2, paint1); 
    canvas.drawLine(x/2, y/2, x/2, y, paint1); 
    canvas.drawLine(x/2, y/2, x/2, -y, paint1); 
} 
+0

那么,这是什么问题? – Altoyyr

+0

@Altoyyr我想要四条线的坐标 – CamlX

+0

所以 - 你想画4条线?一个从圈子中心向上。一个从圈子的中心向下。一个从圆圈的中心向左。还有一个从圈子的中心向右。那是对的吗? –

回答

0

所以,你要4线 - 两条对角线的,一个垂直和水平之一,所有过境通过圆心。

首先,让我们设置的圆心:

int centerX = x/2; 
int centerY = y/2; 

然后,添加一些辅助变量的左,上,右,下了线。我们加一点余量,这样会有余地额外的线路在对角线的两端:

int margin = 10; // Arbitrary value, modify as desired 
int top = 0 + margin; 
int bottom = canvas.getHeight() - margin; 
int left = 0 + margin; 
int right = canvas.getWidth() - margin; 

有了这些,我们可以绘制线条容易:

// Vertical line. X coordinate = center, stretching from "top" to "bottom" 
canvas.drawLine(centerX, top, centerX, bottom); 

// Horizontal line. Y coordinate = center, stretching from "left" to "right". 
canvas.drawLine(left, centerY, right, centerY); 

// First diagonal line. Stretching from the top left corner to the bottom right corner. 
canvas.drawLine(top, left, bottom, right); 

// Second diagonal line. Stretching from the top right corner to the bottom left corner. 
canvas.drawLine(top, right, bottom, left); 

现在加入小线段使它看起来像一个曲线:

int delta = 4; // Arbitrary value, determines how long the small lines will be. 
canvas.drawLine(top, left, top + delta, left - delta); 
canvas.drawLine(top, right, top + delta, right + delta); 
canvas.drawLine(bottom, left, bottom - delta, left - delta); 
canvas.drawLine(bottom, right, bottom - delta, right + delta); 

如果你想曲线是圆的,而不是尖锐的,你有两个选择。
首先是制作透明物体Image并将其放在画布上。您可以制作一条曲线的图像,并将其镜像以获取其他三条曲线。
第二个是使用Path.cubicTo,但这是你可能想要稍后尝试的东西。如果你想了解它,在this Stack Overflow answer中有详细解释。

+0

并且对于长度?,我从圆中画出了四条线,对于其他四条线,它们不是对角线,它们有一定的曲线 – CamlX

+0

@AbbesChouokchou Hm,取决于曲线是如何定义的。如果曲线看起来像一个尖锐的“V”,那么可以通过为'left','right','top'和'bottom'设置较小的值来缩短线条。然后你可以从那里添加一小条额外的线。垂直线条为 –

+0

,如何更改长度? – CamlX