Search code examples
androidandroid-layoutandroid-custom-viewandroid-touch-event

Does an OnClickListener take into account the scaling factor of the view?


I have a CustomLayout view that extends TableLayout and contains LinearLayout row children, each of them containing a RelativeLayout cell (each individual cell having an OnClickListener). The CustomLayout also has a ScaleGestureDetector present and supports scrolling/dragging on the X/Y axis. Everything works great when the scaling factor is 1 (no scaling).

The problem is that each cell's OnClickListener doesn't appear to take into account the scaling factor of the CustomLayout and subsequently, it's own cell scaling. So if the zoom is 2x and you click on the now bigger cell, you actually clicked on the touch target of another, smaller cell from the previous zoom level.

I don't know if this is by design or not though (I've tried to find some docs regarding this issue, but haven't found anything). Any input/ideas on this matter? I know there's always the possibility of implementing the ClickListener manually, by listening on the CustomLayout touch events and mapping the touch event position to the now zoomed in/larger cell. But I wanted to know if it's possible to use the default OnClickListener for an easier and cleaner approach.


Solution

  • Never mind. Apparently I was scaling/translating the canvas instead of the view, so I guess it makes sense that the layout children couldn't take into consideration that the canvas has moved.

    So, either scale/translate the entire view, not only its canvas (but this would result in lots of animations and dropped frames/jerkiness) or scale/translate only the canvas (this provides improved performance) and manage all the touch events, such as clicks, manually.