gpt4 book ai didi

android - 找到两个 View 之间的交集?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:56:30 25 4
gpt4 key购买 nike

如何找到两个 ImageView 之间的交集???

为此我尝试了这个但没有进展

有人可以建议我这样做吗?

代码 fragment

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 文件:

<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>

最佳答案

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

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

关于android - 找到两个 View 之间的交集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com