如何知道触摸位置是否在特定区域 - Android
How to know if touch position is in a specific area - Android
我想在点击屏幕的特定区域(我的 RelativeLayout
)时调用一个方法。我正在开发一个 tic-toc-toe 游戏,我想在每个点击的正方形中制作每个圆圈或十字的 alpha
值。
这是我的 xml
文件,如果点击每个 ImageView
都会变成一个圆圈或十字:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#e4f7a6"
android:layout_above="@+id/buttonRed"
android:id="@+id/relativeLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/board"
android:src="@drawable/board"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/red"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/leftCenter"
android:src="@drawable/red"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:translationX="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rightCenter"
android:src="@drawable/red"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:translationX="-20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upCenter"
android:src="@drawable/red"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:translationY="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upLeft"
android:src="@drawable/red"
android:layout_alignParentLeft="true"
android:translationX="20dp"
android:translationY="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upRight"
android:src="@drawable/red"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:translationY="40dp"
android:translationX="-20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/downLeft"
android:src="@drawable/red"
android:translationY="-40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:translationX="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/downRight"
android:src="@drawable/red"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:translationY="-40dp"
android:translationX="-20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/downCenter"
android:src="@drawable/red"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:translationY="-40dp"
/>
</RelativeLayout>
我知道如何通过以下方式获取点击点位置:
Display display = getWindowManager().getDefaultDisplay();
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
return true;
}
那么我应该如何得到 tic toc toc toe 棋盘中每个方格的面积?
将焦点更改监听器应用于图像视图,并在图像视图将获得焦点的位置执行代码,例如:-
if(hasFocus){
// do your code
}else{
}
我想在点击屏幕的特定区域(我的 RelativeLayout
)时调用一个方法。我正在开发一个 tic-toc-toe 游戏,我想在每个点击的正方形中制作每个圆圈或十字的 alpha
值。
这是我的 xml
文件,如果点击每个 ImageView
都会变成一个圆圈或十字:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#e4f7a6"
android:layout_above="@+id/buttonRed"
android:id="@+id/relativeLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/board"
android:src="@drawable/board"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/red"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/leftCenter"
android:src="@drawable/red"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:translationX="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rightCenter"
android:src="@drawable/red"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:translationX="-20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upCenter"
android:src="@drawable/red"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:translationY="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upLeft"
android:src="@drawable/red"
android:layout_alignParentLeft="true"
android:translationX="20dp"
android:translationY="40dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upRight"
android:src="@drawable/red"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:translationY="40dp"
android:translationX="-20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/downLeft"
android:src="@drawable/red"
android:translationY="-40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:translationX="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/downRight"
android:src="@drawable/red"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:translationY="-40dp"
android:translationX="-20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/downCenter"
android:src="@drawable/red"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:translationY="-40dp"
/>
</RelativeLayout>
我知道如何通过以下方式获取点击点位置:
Display display = getWindowManager().getDefaultDisplay();
@Override
public boolean onTouchEvent(MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
return true;
}
那么我应该如何得到 tic toc toc toe 棋盘中每个方格的面积?
将焦点更改监听器应用于图像视图,并在图像视图将获得焦点的位置执行代码,例如:-
if(hasFocus){
// do your code
}else{
}