在两个中心(视图)之间绘制线条,而不是自己绘制视图

时间:2014-04-13 11:36:33

标签: android canvas views line

我有一个布局,我需要在两个子视图之间画线(在两个中心之间),但是看起来与自己的视图重叠的线

我使用以下方法成功地在两个中心之间画线:

final int lineStartX = ((LayoutParams) viewA.getLayoutParams()).leftMargin + (viewA.getMeasuredWidth() / 2);
final int lineStartY = ((LayoutParams) viewA.getLayoutParams()).topMargin + (viewA.getMeasuredHeight() / 2);
final int lineEndX = ((LayoutParams) viewB.getLayoutParams()).leftMargin + (viewB.getMeasuredWidth() / 2);
final int lineEndY = ((LayoutParams) viewB.getLayoutParams()).topMargin + (viewB.getMeasuredHeight() / 2);
canvas.drawLine(lineStartX, lineStartY, lineEndX, lineEndY, mLinePaint);

但是视线区域也可以看到这条线。

在线上绘制视图不被视为解决方案,因为视图背景应该是透明的

我如何实现这一目标?

提前致谢!

enter image description here

2 个答案:

答案 0 :(得分:0)

好吧,我终于用我自己的类似三角公式实现了它: http://www.mathopenref.com/similartriangles.html

答案 1 :(得分:0)

lineStartX = viewA.getX + viewA.getMeasuredWidth() / 2;
// Start from bottom of view
lineStartY = viewA.getY + viewA.getMeasuredHeight;
lineEndX = viewB.getX + viewB.getMeasuredWidth() / 2;
// End at top of next view
lineEndY = viewB.getY;
canvas.drawLine(lineStartX, lineStartY, lineEndX, lineEndY, mLinePaint);

如果您的视图是从上到下的,这将起作用。它从顶视图的底部中间开始,并在底视图的顶部中间画一条线。