Android 上可以处理不规则形状上的事件吗?
据我目前所知(了解很少),Android 中的所有视图都是正方形或矩形。这几乎一直都很好,直到您想要(我真正想要的)创建可以处理事件的非方形形状。
我的目标是将一个圆分为 3 个部分,每个部分 120°。圆圈的每个部分都应该像一个按钮。问题是,如果我们查看圆的三分之二并将它们放在严格包含它们的方框中,它们会相互重叠:不切实际地知道用户要单击哪个...
我尝试使用自定义视图我在其中绘制了部分,但事件在视图的所有表面上触发。
任何建议或方向都非常受欢迎。
谢谢,保罗
As far as I know for now (which is little), all the Views in Android have square or rectangular shapes. This is fine almost all the time until you want - what I actually want - to create non-square shapes that could handle events.
My goal is to have a circle divided in 3 sections of 120° each. Each section of the circle should behave like a button. The problem is that if we look at those 3rds of a circle and place them in a square box that strictly contains them, they are overlapping each other: not practical to know which the user meant to click...
I tried with a custom view in which I drew my sections but the events are triggering on all the surface of the view.
Any suggestion or direction is very welcome.
Thx, Paul
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信处理此问题的方法是重写每个控件上的
onTouch
,对触摸坐标执行您自己的几何检查,如果它位于自定义区域之外返回 false
这将将事件传递到另一个视图。否则返回调用该方法的超级版本。我不是 100% 确定,所以如果我错了,请有人纠正我,但这可能值得一试。I believe the way to handle this is to override
onTouch
on each control, perform your own geometric checks on the touch coordinates, and if it's outside the custom areareturn false
which will pass over the event to another view. Otherwise return invoking the super version of the method. I'm not 100% sure so someone please correct me if I'm wrong, but it could be worth a try.我不知道您是否可以显式创建这些形状,但肯定有一种方法可以在自定义视图中使用它。但这是相当困难的。
首先,您需要通过设置
OnTouchListener
来挂钩OnTouch
事件,更多此处。最简单的方法是仅对MotionEvent
的ACTION_UP
操作做出反应,可通过MotionEvent.getAction()
访问,详细信息请参见 此处。然后,您可以通过getX()
和getY()
获取此MotionEvent
中事件发生的 X 和 Y 坐标。现在开始数学...您现在必须计算点击发生在哪个部分。为此,您需要自定义视图的大小(以像素为单位)及其位置(左上角),恐怕我现在无法告诉您适当的方法......您必须挖掘它们......假设圆的中心始终位于视图的中心,并且圆完全延伸到视图边界,您现在可以计算扇区。
我们将事件的 X 和 Y 坐标分别称为
eventX
和eventY
,并让centerX
和centerY
为圆心坐标以及radius
是圆的半径。首先检查事件是否发生在圆内,然后计算角度:
最后,
total
应该是辐射点中点击的顺时针角度,您可以根据您的部分;)如果有问题请纠正我^^I don't know whether you can create those shapes explicitly but there would definitely be a way to use this in a custom view. But this is quite difficult.
First you would need to hook on to the
OnTouch
event via setting anOnTouchListener
, more here. The easiest way would be to only react onACTION_UP
actions of theMotionEvent
, accessable viaMotionEvent.getAction()
, details over here. Of thisMotionEvent
you can then get the X and Y coordinates, where the event occured viagetX()
andgetY()
.Now begins the maths... you now have to calculate in which section the click occured. To do this, you need the size of your custom view in pixels and its location (upper left corner), I'm afraid I can't tell you the appropriate methods right now... you would have to dig them up... Assuming the center of your circle is always in the center of your view and that the circle extends fully to the views boundaries, you can now calculate the sector.
Let's call the events X and Y coordinates
eventX
andeventY
respectively and letcenterX
andcenterY
be the circles center coordinates as well asradius
being the circles radius.First check, whether the event occured in the circle, then calculate angle:
In the end,
total
should be the clockwise angle of the click in radiants which you can check against your sections ;) Correct me if there is something wrong^^首先非常感谢您的所有建议,这些建议帮助我实现了目标。
由于无法触发形状上的事件,因此在包含形状的视图上使用 onTouch() 是可行的方法。
接下来是运行它所需的一切。
首先,自定义视图 Zones.java:
其次,主要活动 Design.java:
... 以及主 xml 视图(嵌入我们的自定义类视图) main.xml
任何评论表示赞赏!
保罗:)
First thank you a lot for all your suggestions which helped me to reach the goal.
Since there is no way to trigger events on shapes, using onTouch() on the view which contains the shapes is the way to go.
Following, all you need to run it.
First, the custom view Zones.java:
Second, the main activity, Design.java:
... and the main xml view (which embeds our custom class view) main.xml
Any comments appreciated!
Paul :)