[英]How to connect two buttons with a line in Android?
I want to connect two opposite radiobuttons or normal Buttons with a line in Canvas like this: image我想像这样在画布中用一条线连接两个相反的单选按钮或普通按钮:图像
I have the FingerLine Class to draw the red line and in the layout I have 3 columns (RecyclerView,FingerLine,RecyclerView).我有 FingerLine 类来绘制红线,在布局中我有 3 列(RecyclerView、FingerLine、RecyclerView)。 I want to develop an application to match two image with lines.
我想开发一个应用程序来匹配两个图像与线条。
Currently what I do is draw in the middle but I would like the drawing line to go from one button to another I don't know how to do it.目前我所做的是在中间画画,但我希望画线从一个按钮到另一个按钮,我不知道该怎么做。
public class FingerLine extends View {
private final Paint mPaint;
private float startX;
private float startY;
private float endX;
private float endY;
float joinX, joinY = 0;
ArrayList<Line> lines = new ArrayList<Line>();
class Line {
float startX, startY, stopX, stopY;
public Line(float startX, float startY, float stopX, float stopY) {
this.startX = startX;
this.startY = startY;
this.stopX = stopX;
this.stopY = stopY;
}
public Line(float startX, float startY) { // for convenience
this(startX, startY, startX, startY);
}
}
public FingerLine(Context context) {
this(context, null);
}
public FingerLine(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Style.STROKE);
mPaint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Line l : lines) {
canvas.drawLine(l.startX, l.startY, l.stopX, l.stopY, mPaint);
}
}
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (joinX <= 0 || joinY <= 0) {
lines.add(new Line(event.getX(), event.getY()));
}else{
lines.add(new Line(event.getX(), event.getY()));
}
break;
case MotionEvent.ACTION_MOVE:
if(lines.size() > 0) {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
invalidate();
break;
}
case (MotionEvent.ACTION_CANCEL):
break;
case (MotionEvent.ACTION_OUTSIDE):
break;
case MotionEvent.ACTION_UP:
if(lines.size() > 0) {
Line current = lines.get(lines.size() - 1);
current.stopX = event.getX();
current.stopY = event.getY();
joinX = event.getX();
joinY = event.getY();
invalidate();
break;
}
}
return true;
}
}
I think for first you have to find an image of a line.我认为首先你必须找到一条线的图像。 then you have to create 6(or how many you want)
imageview
s with the image of line and then make them invisible with android:visibility = "invisible"
.然后你必须用线条图像创建 6 个(或你想要的数量)
imageview
s,然后使用android:visibility = "invisible"
使它们不android:visibility = "invisible"
。 then you have to find two checked radiobutton
s in your activity.java and then set align_right
and align_left
to the checked radiobutton
s for your imageview
in activity.java and then make it visible.那么你必须在你的 activity.java 中找到两个选中的
radiobutton
s,然后将align_right
和align_left
设置为你在 activity.java 中的imageview
的选中的radiobutton
s,然后使其可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.