I am facing problems drawing arrows between two or more rectangle objects. I have a Array list storing several rectangles.
For instance, the user have two rectangles on the canvas. Upon clicking the draw arrow button, the user would be able to click on the first rectangle and drag to the second rectangle and the arrow will form between the two rectangles.
I have no idea how to fetch the touch coordinate in order to draw the arrows. I am able to fetch the current x and y position. Also, how can I redraw the arrow if my rectangles got shifted.
I only manage to draw the arrow head.
this an image of what i want to achieve:
my codes are as follows:
public static ArrayList<RectF> rects = new ArrayList<RectF>();
public void addRectangle() {
RectF rect = new RectF();
rect.set(x, y, x + 80, y + 50);
rects.add(rect);
}
public void addArrow() {
//arrow head
Point a = new Point(0, 0);
Point b = new Point(0, 25);
Point c = new Point(21, 12);
Path path = new Path();
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(a.x, a.y);
path.close();
paths.add(path);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
for (RectF rec : rects) {
canvas.drawRect(rec, paintColor);
}
}
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
for (int i = 0; i < rects.size(); i++) {
// Equation Editor
if (rects.get(i).contains(x, y)) {
}
}
}