0

Can non-square hit areas be done on android? i.e for making a n-segmented pie chart clickable. This was th default behaviour on ActionScript, I can't believe it's just not a thing on Android but that seems to be the case.

Similar to pie chart, what if we have a target type shape ( concentric nested circles ) and we want to detect which ring in the target has been touched, are we seriously limited to square hit areas?

I've seen one proposed solution which is to use colour, but I have instances where the color is identical on different shapes, so this would not work.

It's 2021, I can't beleive I can do something easily on ancient ActionScript but not on swanky Android. Am I missing something?

As a tangible example - a pie wedge using drawarc on the onDraw method of a class...

    var oval = RectF(0f, 0f, _size, _size)
    canvas.drawArc(oval, 0f, _arc.toFloat(), true, paint)

In the parent piechart class (we we instantiate all these wedges), how would we get hit detection within the precise area of the wedge?

Gary Frewin
  • 505
  • 3
  • 16
  • You can get the touch coordinates via `MotionEvent` how you handle it in your custom layout/view/app is up to you. – Morrison Chang Feb 23 '21 at 08:09
  • "Can it be done?" I suppose so, if you are able to describe the area as a polygon. For example Java has a [Polygon](https://docs.oracle.com/javase/7/docs/api/java/awt/Polygon.html) class with a contains() method. So you can always implement a custom View to get all touch events and then handle the event depending on which polygon was touched. – Bö macht Blau Feb 23 '21 at 08:11
  • Also be aware that unlike pixel accurate mouse, mobile has fat fingers so the idea of a rectangular hit area isn't so odd: [What exactly is “Touch Target Size” in Android Material Design](https://stackoverflow.com/q/37517021/295004). Also: [Check if a point is within an ellipse](https://math.stackexchange.com/q/76457/118981) and [How to determine whether a point (X,Y) is contained within an arc section of a circle (i.e. a Pie slice)?](https://stackoverflow.com/q/6270785/295004) – Morrison Chang Feb 23 '21 at 08:35
  • The pie chart class is one view and you may have a dead zone in the center to avoid fat fingers selecting the wrong slice. – Morrison Chang Feb 23 '21 at 08:42
  • So without having a solid grasp of maths and doing it manually, like in the examples, there is no straightforward way to make the hit area of a view actually match the shape of the view by default? A poor show from Android Dev. :/ – Gary Frewin Feb 23 '21 at 08:56
  • Views were in Android from 2008 and base GUI elements have been to my knowledge rectangular. If you don't have the math skills, find a library (or search). The framework is doing a lot under the hood for rendering, see: [Drawn out: How Android renders (Google I/O '18)](https://youtu.be/zdQRIYOST64) – Morrison Chang Feb 23 '21 at 09:31

0 Answers0