簡體   English   中英

如何知道觸摸位置是否在特定區域中-Android

[英]How to know if touch position is in a specific area - Android

如果要輕按屏幕的特定區域(我的RelativeLayout ),我想調用一個方法。 我正在開發井字游戲,我想在每個點擊的正方形中制作每個圓圈或十字的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;
}

那么,我該如何在井字腳趾板上獲得每個正方形的面積?

將焦點更改列表器應用於imageview並執行代碼,以使image view獲得焦點,例如:

if(hasFocus){
// do your code

}else{

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM