5

How can find the intersection between two ImageView ???

For this I tried this but no progress

Can anyone suggest me to do this?

Code snnipet

imageView = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);

    Rect rect =new Rect();
    imageView.getHitRect(rect);

    Rect rect1 = new Rect();
    imageView2.getHitRect(rect1);

    if(Rect.intersects(rect, rect1)){
        Toast.makeText(this, "hoho", Toast.LENGTH_SHORT).show();
    }

xml file :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IntersectActivity" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="121dp"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>
4

1 回答 1

3

基本上,在 中onCreate(),android 尚未创建视图,因此任何尝试获取点的宽度高度位置等都将不起作用。

相反,覆盖onWindowFocusChanged(boolean focus)并将您的代码放在那里

于 2013-10-02T18:43:08.920 回答