I need help getting two ImageViews to collide, I looked around on this website and many youtube videos and think I have found one solution to my problem. I found some code from another person post,
how to detect when a ImageView is in collision with another ImageView?
and I'm just wondering where I should place that code in my program because when it's at the bottom i try to log.d to show if I was succesful on trying to detect whether the imageViews collided and nothing shows. Anyways here is my code and the code I used from the other question is at the very bottom and used as a comment. YOUR HELP IS EXTREMELY APPRECIATED, THANK YOU IF YOU HELPED ME!
Main.java
package com.example.admin.basketball;
import android.graphics.Point;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Layout
private RelativeLayout myLayout = null;
//Screen Size
private int screenWidth;
private int screenHeight;
//Position
private float ballDownY;
private float ballDownX;
//Initialize Class
private Handler handler = new Handler();
private Timer timer = new Timer();
//Images
private ImageView net = null;
private ImageView ball = null;
//for net movement along x-axis
float x;
float y;
//points
private int points = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLayout = (RelativeLayout) findViewById(R.id.myLayout);
//score
final TextView score = (TextView) findViewById(R.id.score);
//imageviews
net = (ImageView) findViewById(R.id.net);
ball = (ImageView) findViewById(R.id.ball);
//retrieving screen size
WindowManager wm = getWindowManager();
Display disp = wm.getDefaultDisplay();
Point size = new Point();
disp.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
//move to out of screen
ball.setX(-80.0f);
ball.setY(screenHeight + 80.0f);
//start timer
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
changePos();
}
});
}
}, 0, 20);
}
public void changePos() {
//down
ballDownY += 10;
if (ball.getY() > screenHeight) {
ballDownX = (float) Math.floor((Math.random() * (screenWidth -
ball.getWidth())));
ballDownY = -100.0f;
}
ball.setY(ballDownY);
ball.setX(ballDownX);
//make net follow finger
myLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
MainActivity.this.x = event.getX();
y = event.getY();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
net.setX(MainActivity.this.x);
net.setY(y);
}
return true;
}
});
}
}
/*
private boolean viewsOverlap(ImageView net, ImageView ball) {
int[] net_coords = new int[2];
net.getLocationOnScreen(net_coords);
int net_w = net.getWidth();
int net_h = net.getHeight();
Rect net_rect = new Rect(net_coords[0], net_coords[1], net_coords[0] +
net_w, net_coords[1] + net_h);
int[] ball_coords = new int[2];
ball.getLocationOnScreen(ball_coords);
int ball_w = ball.getWidth();
int ball_h = ball.getHeight();
Rect ball_rect = new Rect(ball_coords[0], ball_coords[1], ball_coords[0]
+ ball_w, ball_coords[1] + ball_h);
return net_rect.intersect(ball_rect) || net_rect.contains(ball_rect) ||
ball_rect.contains(net_rect);
}*/