-1

I have two images and i want check the intersection that. But my code not working! Please, help me!

public class MainActivity extends Activity {
ImageView img1, img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    onWindowFocusChanged(true);
    AddEvents();
}
public void Form(){
    img1=(ImageView) findViewById(R.id.imageView1);
    img2=(ImageView) findViewById(R.id.imageView2);
}

public void AddEvents(){
    Form();
    Rect r1= new Rect();
    img1.getHitRect(r1);
    Rect r2= new Rect();
    img2.getHitRect(r2);
    if(Rect.intersects(r1,r2)){
        Toast.makeText(this, "intersected", Toast.LENGTH_LONG).show();
    }
}

}

1 Answers1

1

According to the documentation, you are doing it correctly and they do NOT intersect:

http://developer.android.com/reference/android/graphics/Rect.html#intersects(android.graphics.Rect, android.graphics.Rect)

The problem is that getHitRect is probably not doing what you expect it to do. It is designed to make the "hit" area (touch area) a different size than the object. Also, if you check it before the layout is done, it will not return anything useful.

See this post:

gethitRect() , I am doing it wrong, how does this work?

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36