Home > database >  HitRect returning wrong Y value for unknown reason
HitRect returning wrong Y value for unknown reason

Time:08-19

I am creating a pixel art app, which has the following layout:

enter image description here

Input events are detected inside the pixel art board, meaning that if the user swipes from the root layout and travels their finger inside the pixel art board, it doesn't detect it. This is obviously a minor issue.

To fix this, I looked online and I found the following code which kind of fixed the problem:

binding.root.setOnTouchListener { _, motionEvent ->
    val hitRect = Rect()
    binding.activityCanvasCardView.getHitRect(hitRect)

    if (hitRect.contains(motionEvent.x.toInt(), motionEvent.y.toInt())) {
        Log.d("LOG123", "Hi ${motionEvent.xPrecision} ${motionEvent.yPrecision}")

        binding.activityCanvasPixelGridView.onTouchEvent(motionEvent)
    }
    true
}

Note that the view coordinates are converted into pixel coordinates in the onTouchEvent method.

Simple enough, right? In a perfect world, that code should fix the issue.

There's only one problem, for some reason, there is an offset with the y value:

enter image description here

I am unsure why I am having this strange delay with the Y coordinates.

I've tried for a couple of hours to fix this issue, some of the things I tried were:

  • manually applying offset values
  • using different rect functions of the View class
  • look online to see if anyone has a similar issue

Any help in regards to this would be appreciated, completely unsure why this is happening as I'm following things by the book. This issue is very annoying and frustrating

  • Related