1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Android】タップされた座標にあるビューが知りたい

Last updated at Posted at 2020-06-23

ソース

class MainActivity : Activity() {

    private var views = mutableSetOf<View>()
    private var debugCount = 0

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // タップ時に判定したいViewを詰める
        views.add(findViewById(R.id.redView))
        views.add(findViewById(R.id.purpleView))
        views.add(findViewById(R.id.greenView))
        views.add(findViewById(R.id.yellowView))

        findViewById<ScrollView>(R.id.scrollView).setOnTouchListener(View.OnTouchListener(
            fun (view, event): Boolean {
                if (event.action == MotionEvent.ACTION_DOWN) {
                    val x = event.rawX.toInt()
                    val y = event.rawY.toInt()

                    for (view in views) {
                        if (getRect(view).contains(x, y)) {
                            Log.d("", "${debugCount++}: ${resources.getResourceEntryName(view.id)}がタップされた。")
                            break
                        }
                    }
                }

                return false
            }
        ))
    }

    private fun getRect(view: View): Rect {
        val rect = Rect()
        view.getGlobalVisibleRect(rect)
        return rect
    }
}

レイアウト

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="100dp">

            <View
                android:id="@+id/redView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginStart="72dp"
                android:layout_marginLeft="72dp"
                android:layout_marginTop="152dp"
                android:background="#F44336"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="211dp"
                android:layout_height="179dp"
                android:layout_marginTop="312dp"
                android:layout_marginEnd="44dp"
                android:layout_marginRight="44dp"
                android:background="#FFC107"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <View
                    android:id="@+id/purpleView"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginTop="36dp"
                    android:layout_marginEnd="56dp"
                    android:layout_marginRight="56dp"
                    android:background="#9C27B0"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>

            <View
                android:id="@+id/greenView"
                android:layout_width="200dp"
                android:layout_height="100dp"
                android:layout_marginTop="556dp"
                android:background="#4CAF50"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <View
                android:id="@+id/yellowView"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginTop="820dp"
                android:background="#FFEB3B"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

実際の動作

sample.gif

以上

追記

これ四角形しか判定できないね。素直にタップのイベントを付けてあげましょう。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?