I would start this with pure math!
Assuming that both ImageView1 and ImageView2 do not have cropping (for example, the lines in the images are ImageView diagonal), you can use the width and height of both pic to express both lines in the formulas. Here is an example. NOTE. I use the coordinate system Android → point (0,0) - the upper left corner, while y increases to the bottom.
Click here for a graphical representation.
String1
y = h1 / w1 (x - a1) + b1
Line 2
y = -h2 / w2 (x - a2) + b1 + h2
Now we need a point where line1 = line2, so we get
h1 / w1 (x - a1) + b1 = -h2 / w2 (x - a2) + b1 + h2
If you rewrite the equation you get:
x = (w1 * w2 * (b2 + h2 - b1) + h1 * w2 * a1 + h2 * w1 * a2) / (h1 * w2 + h2 * w1);
Once you know the x coordinate, you can use this to find the y coordinate ... Below is the code:
private void check() { // Setup variables for shorter notation int w1 = mImg1.getWidth(); int h1 = mImg1.getHeight(); int a1 = mImg1.getLeft(); int b1 = mImg1.getTop(); int w2 = mImg2.getWidth(); int h2 = mImg2.getHeight(); int a2 = mImg2.getLeft(); int b2 = mImg2.getTop(); int x = 0; if(h1*w2 + h2*w1 == 0) { // Return to avoid division by zero return; } else { // Calculate the x-value using the re-written formula x = (w1*w2*(b2 + h2 - b1) + h1*w2*a1 + h2*w1*a2) / (h1*w2 + h2*w1); } // Now use the x-value to calculate the y-value int y = h1 / w1 * (x - a1) + b1; Log.d("Output", "x: " + x + " y:" + y); }
NOTE. You might want to set your ImageViews to android: width = "wrap_content" and android: height = "wrap_content". Otherwise, the images are fixed to the size you enter! I tested it with ImageView with a circular background. If you use the calculated xy coordinates, he draws the ball right at the intersection! Good luck.
source share